Jump to content

[SOLVED] Simple but cannot figure it out


galvin

Recommended Posts

I simply have a database with one table called "runningbacks".  There are three fields (playerid, name, team).

 

I simply want to have TWO "select" HTML menus on my webpage where the drop-down list in each has the full list of running backs from the runningbacks table (whether it's 5 or 50). 

 

I know the code I have below is close, but it's not quite doing what I need.  With what I have below, the first "select" menu has info from the runningbacks table in it, but the second one is completely empty.

 

Any idea what I'm doing wrong?

 


<?php

$sql = "SELECT *
FROM runningbacks";
$RBs_set = mysql_query($sql, $connection);
if (!$RBs_set) {
die("Database query failed: " . mysql_error());
} else {

	for($x=1; $x<=2; $x++){

		echo "\n<tr><td>RB</td>";
		echo "\n<td><select name=\"RunningBacks\"/>";
		echo "\n<option class='' value=''></option>";
			while ($RBsArray = mysql_fetch_array($RBs_set)) {

						for($y=0; $y<count($RBsArray); $y++){
							$playerid = $y+1;
							echo "\n<option class='' value='{$playerid}'>{$RBsArray[$y]}</option>";
						}

			}
		echo "\n</select>";
		echo "\n</td>";
		echo "</tr>";
	}	

}

?>

Link to comment
https://forums.phpfreaks.com/topic/137289-solved-simple-but-cannot-figure-it-out/
Share on other sites

You are not requerying the data. mysql_data_seek

 


<?php

$sql = "SELECT *
FROM runningbacks";
$RBs_set = mysql_query($sql, $connection);
if (!$RBs_set) {
die("Database query failed: " . mysql_error());
} else {

      for($x=1; $x<=2; $x++){
      
         echo "\n<tr><td>RB</td>";
         echo "\n<td><select name=\"RunningBacks\"/>";
         echo "\n<option class='' value=''></option>";
         mysql_data_seek($RBs_set, 0); // reset pointer to 0.   
         while ($RBsArray = mysql_fetch_array($RBs_set)) {
         
                     for($y=0; $y<count($RBsArray); $y++){
                        $playerid = $y+1;
                        echo "\n<option class='' value='{$playerid}'>{$RBsArray[$y]}</option>";
                     }
                     
            }
         echo "\n</select>";
         echo "\n</td>";
         echo "</tr>";
      }   
   
}

?>

That did it, but one other thing.  In that same code, it seems to think there are 6 items in the $RBsArray, when I feel like there should only be 3 (since there are only 3 fields in the table).  The HTML output is the following.  What's with all those "undefined offsets" on line 41. 

 

FYI, line 41 of the code is...echo "\n<option class='' value='{$playerid}'>{$RBsArray[$y]}</option>";

 

 



<tr><td>RB</td>
<td><select name="RunningBacks"/>
<option class='' value=''></option>
<option class='' value='1'>1</option>
<option class='' value='2'>Brian Westbrook</option>
<option class='' value='3'>Eagles</option><br />
<b>Notice</b>:  Undefined offset:  3 in <b>C:\wamp\www\rotocourt\index.php</b> on line <b>41</b><br />

<option class='' value='4'></option><br />
<b>Notice</b>:  Undefined offset:  4 in <b>C:\wamp\www\rotocourt\index.php</b> on line <b>41</b><br />

<option class='' value='5'></option><br />
<b>Notice</b>:  Undefined offset:  5 in <b>C:\wamp\www\rotocourt\index.php</b> on line <b>41</b><br />

<option class='' value='6'></option>
<option class='' value='1'>2</option>
<option class='' value='2'>Adrian Peterson</option>
<option class='' value='3'>Vikings</option><br />
<b>Notice</b>:  Undefined offset:  3 in <b>C:\wamp\www\rotocourt\index.php</b> on line <b>41</b><br />

<option class='' value='4'></option><br />
<b>Notice</b>:  Undefined offset:  4 in <b>C:\wamp\www\rotocourt\index.php</b> on line <b>41</b><br />

<option class='' value='5'></option><br />
<b>Notice</b>:  Undefined offset:  5 in <b>C:\wamp\www\rotocourt\index.php</b> on line <b>41</b><br />

<option class='' value='6'></option>
<option class='' value='1'>3</option>
<option class='' value='2'>Michael Turner</option>
<option class='' value='3'>Falcons</option><br />
<b>Notice</b>:  Undefined offset:  3 in <b>C:\wamp\www\rotocourt\index.php</b> on line <b>41</b><br />

<option class='' value='4'></option><br />
<b>Notice</b>:  Undefined offset:  4 in <b>C:\wamp\www\rotocourt\index.php</b> on line <b>41</b><br />

<option class='' value='5'></option><br />
<b>Notice</b>:  Undefined offset:  5 in <b>C:\wamp\www\rotocourt\index.php</b> on line <b>41</b><br />

<option class='' value='6'></option>
<option class='' value='1'>4</option>
<option class='' value='2'>Larry Johnson</option>
<option class='' value='3'>Chiefs</option><br />
<b>Notice</b>:  Undefined offset:  3 in <b>C:\wamp\www\rotocourt\index.php</b> on line <b>41</b><br />

<option class='' value='4'></option><br />
<b>Notice</b>:  Undefined offset:  4 in <b>C:\wamp\www\rotocourt\index.php</b> on line <b>41</b><br />

<option class='' value='5'></option><br />
<b>Notice</b>:  Undefined offset:  5 in <b>C:\wamp\www\rotocourt\index.php</b> on line <b>41</b><br />

<option class='' value='6'></option>
<option class='' value='1'>5</option>
<option class='' value='2'>DeAngelo Williams</option>
<option class='' value='3'>Panthers</option><br />
<b>Notice</b>:  Undefined offset:  3 in <b>C:\wamp\www\rotocourt\index.php</b> on line <b>41</b><br />

<option class='' value='4'></option><br />
<b>Notice</b>:  Undefined offset:  4 in <b>C:\wamp\www\rotocourt\index.php</b> on line <b>41</b><br />

<option class='' value='5'></option><br />
<b>Notice</b>:  Undefined offset:  5 in <b>C:\wamp\www\rotocourt\index.php</b> on line <b>41</b><br />

<option class='' value='6'></option>
</select>

 

 

 

What exactly is your goal here?

 

You are counting the number of columns in the array. Your table seems to have 6 columns.

 

         $playerid = 0;
         while ($RBsArray = mysql_fetch_array($RBs_set)) {
                $playerid++;
                echo "\n<option class='' value='{$playerid}'>" . $RBsArray[0] . "</option>";
            }

 

Unsure if that is exactly what you want, but yea. If that is not it post what you want the end select to look like and the table structure/example data of data in your database.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.