Array

  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

Hello, I'm having a problem that I just can't figure out. Keep in mind, that I've only been programming a month. So, I did a DB query of my users table, I put that result into a array and then I generated a drop down list from that array. It worked perfectly for the first drop down list. I need to do this for other drop down list on the same page; however, whenever I try to display the results again in the drop down list, its not working in any other list. Please see my code and please help!!!

Code: [ Select ]
<?php
    $result = mssql_query("SELECT * FROM SNP_Users", $connection);
    if (!$result) {
        die("Database query failed: " . mssql_error());
    }
?>
<tr>
    <td><p>Intro<br />
     Assigned:</p></td>
    <td width="50"><div align="center">N/A</div></td>
    <td width="50"><div align="center">N/A</div></td>
    <td width="50"><p align="center">
        <input type="checkbox" name="check[]" id="checkbox1" />
        <br />
        <select name="user1" class="style5">
        <?php
            while ($row = mssql_fetch_array($result)) {
                echo "<option value=\"{$row[1]}\">{$row[1]}</option>";
            }
        ?>
        </select>
    </p></td>
    <td width="50"><p align="center">
        <input type="checkbox" name="check[]" id="checkbox2" />
        <br />
        <select name="user2" class="style5">
        <?php
            echo "<option value=\"{$row[1]}\">{$row[1]}</option>";
        ?>
        </select>
    </p></td>
  1. <?php
  2.     $result = mssql_query("SELECT * FROM SNP_Users", $connection);
  3.     if (!$result) {
  4.         die("Database query failed: " . mssql_error());
  5.     }
  6. ?>
  7. <tr>
  8.     <td><p>Intro<br />
  9.      Assigned:</p></td>
  10.     <td width="50"><div align="center">N/A</div></td>
  11.     <td width="50"><div align="center">N/A</div></td>
  12.     <td width="50"><p align="center">
  13.         <input type="checkbox" name="check[]" id="checkbox1" />
  14.         <br />
  15.         <select name="user1" class="style5">
  16.         <?php
  17.             while ($row = mssql_fetch_array($result)) {
  18.                 echo "<option value=\"{$row[1]}\">{$row[1]}</option>";
  19.             }
  20.         ?>
  21.         </select>
  22.     </p></td>
  23.     <td width="50"><p align="center">
  24.         <input type="checkbox" name="check[]" id="checkbox2" />
  25.         <br />
  26.         <select name="user2" class="style5">
  27.         <?php
  28.             echo "<option value=\"{$row[1]}\">{$row[1]}</option>";
  29.         ?>
  30.         </select>
  31.     </p></td>


So, I assumed that since I declared $row in the first drop down, all I would need to do is display the varible in the second drop down, but no matter what I try, I can't get it to work. PLEASE HELP!!!

Thank you,
Jordon Shaw

P.S. I know the code on the checkboxes isn't correct right now, but I'm just trying to figure out the drop boxes right now!
  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

Ok, I've figured out that I will also need to loop through the data for the second drop down; however, at the end of the first loop, the loop is finished and I need a way to reset it back to the beginning, so that I can loop through it again for the second drop down. I've tired reset($result); but that isn't working. No matter what I try, I can not get data in my second drop down!

PLEASE HELP!!!

Jordon
  • IcyDragoon
  • Student
  • Student
  • IcyDragoon
  • Posts: 65

Post 3+ Months Ago

don't know what you are trying to do, but

you can always try to do something like this:

Code: [ Select ]
 
for($i=0; $i<100; ++$i){
  echo "This i: $i\n";
  for($k=0; $k<$i; ++$k){
    if($k==$i%10)
     echo "Yahoo!\n";
    echo "This is k: $k\n";
}
 
  1.  
  2. for($i=0; $i<100; ++$i){
  3.   echo "This i: $i\n";
  4.   for($k=0; $k<$i; ++$k){
  5.     if($k==$i%10)
  6.      echo "Yahoo!\n";
  7.     echo "This is k: $k\n";
  8. }
  9.  


Nested loops

PS: you will get a lot of ks if you run this ....
  • alex89
  • Bronze Member
  • Bronze Member
  • User avatar
  • Posts: 239
  • Loc: Western Australia

Post 3+ Months Ago

The following should work (untested):

Code: [ Select ]
    </p></td>
    <td width="50"><p align="center">
        <input type="checkbox" name="check[]" id="checkbox2" />
        <br />
        <select name="user2" class="style5">
        <?php
                foreach ($row as $value){
                echo "<option value=\"{$value}\">{$value}</option>";
                }
        ?>
        </select>
    </p></td>
  1.     </p></td>
  2.     <td width="50"><p align="center">
  3.         <input type="checkbox" name="check[]" id="checkbox2" />
  4.         <br />
  5.         <select name="user2" class="style5">
  6.         <?php
  7.                 foreach ($row as $value){
  8.                 echo "<option value=\"{$value}\">{$value}</option>";
  9.                 }
  10.         ?>
  11.         </select>
  12.     </p></td>


Read up on foreach loops here: http://www.tutorialspoint.com/php/php_loop_types.htm

Any problems, give me a yell :)
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

reset is for arrays. Even though SQL result is an array, the MySQL pointer in the database is set at the last result, and thus, giving you no results since all of them has already being printed.

Instead of reset($array) you should use mysql_data_seek($result, 0); That 0 brings the MySQL pointer to the first row in the result set... which should populate the SQL result array and give you results to work with.

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 22 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.