Originally posted by Jimbo17
Okay, couple of questions about that code.
1.) what does => mean? I have seen it a lot, but never have figured out what it is for.
The => is a special operator in PHP that is used for arrays. There are two places you can use it, 1) when creating an array, and 2) when using a foreach() loop. All it really does is let's you use/specify an index as well as a value for an array, rather than just a value.
Example:
<?php
// example of creating an array without =>
$sample = array(1, 2, 3, 4, 5);
?>
This would produce an array like this:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
The => above is just how arrays are printed in PHP... it signifies that index '0' has a value of '1'.
However, if you wanted to specify an index for the values above, you could do the following:
<?php
// example of creating an array with =>
$sample = array('first' => 1, 'second' => 2, 'third' => 3, 'fourth' => 4, 'fifth' => 5);
?>
This would create the following array:
Array
(
[first] => 1
[second] => 2
[third] => 3
[fourth] => 4
[fifth] => 5
)
That way you can use => to create an array with your own indexes. There is another way to do the array above, without using the => operator (just so you know).
<?php
// making custom indexes without =>
$sample[ 'first' ] = 1;
$sample[ 'second' ] = 2;
$sample[ 'third' ] = 3;
$sample[ 'fourth' ] = 4;
$sample[ 'fifth' ] = 5;
?>
This will produce the exact same array as above. The second usage for => is in foreach() loops. With the array above, you could do the following:
<?php
foreach ($sample as $number) {
print "$value<BR>\n";
}
?>
That would output the following:
1
2
3
4
5
However, you might want to get the index of each entry, which is where the => comes in.
<?php
foreach ($sample as $index => $number) {
print "$index: $number<BR>\n";
}
?>
That code would output:
first: 1
second: 2
third: 3
fourth: 4
fifth: 5
So that is what => does.
Originally posted by Jimbo17
2.) would I use $results[x][x] (x=any muber within the range of entries I have in the dtatbase) to spit the information back out?
Currently, $results is just an array of the driver's names. If you wanted to have all of the driver's information available, you could do the following:
<?php
// Create an array to hold the results
$results = array();
// Query
$result = mysql_query("SELECT * FROM `d_points` ORDER BY driver_points DESC")or die(mysql_error());
// Check if query is empty
if (mysql_num_rows($result) > 0) {
// Loop results
while($row = mysql_fetch_array($result)) {
// Insert results into the array
$results[$row['driver_id']] = $row; // this line changed
}
// Now you have all the results with ONE query, not 6 : )
} else {
// No Results
}
?>
Note the line that changed above, the line inside the while() loop. Now, you have an array where the index is the driver's ID number, and the value is the entire array of driver information. So now you could do something like this (where x is the driver's id):
<?php
print $results[ $x ][ 'driver_name' ]; // will print the driver's name
print $results[ $x ][ 'driver_id' ]; // will print the driver's ID
print $results[ $x ][ 'driver_address' ]; // will print address (assuming the 'driver_address' field is in the database)
?>
So you have access to all of the driver's information at any point in the script, just by using the driver's ID number in place of x.
Originally posted by Jimbo17
3.) <?= $driver_id ?> is that just what you see in the url? example: newreply.php?s=&action=newreply&threadid=10253854
The <?= $driver_id ?> you are talking about below is referencing the index of the array (which is the driver's id). See how in the foreach loop it is (I changed it from $driver_name to just $driver because you have all of the driver information now):
<?php
foreach ($results as [b]$driver_id[/b] => $driver) {
?>
What that line does is, it loops through all the entries in the $results array. Every time it goes through it, it sets $driver_id to the index, which is the driver's id. It also sets $driver to the entire row pulled from the database.
Compare this line to the code above where you saving the driver's information.
<?php
// Insert results into the array
$results[$row['driver_id']] = $row; // this line changed
?>
That saves the driver info into $results with the driver's ID as the index. So the array will look like:
Array
(
[10] => Array
(
[driver_id] => 10
[driver_name] => John Doe
[...] => ... // other fields
)
[15] => Array
(
[driver_id] => 15
[driver_name] => Jane Doe
[...] => ... // other fields
)
)
Notice how the index in the first array is the same as the driver's id? So in the foreach loop, $driver_id is set to the index of the array (in the above example, 10 and 15), while $driver is being set to the driver's 'row'.
The revised code would look like this:
<SELECT name=driver_1>
<?php
foreach ($results as $driver_id => $driver) {
?>
<OPTION value="<?= $driver_id ?>"><?= $driver[ 'driver_name' ] ?></OPTION>
<?php
}
?>
</SELECT>
Hope that helps!
-Percy