Jump to content

how to iterate a though a recordset without usin, while ($row = mysql_fetch_arra


otuatail

Recommended Posts

I have a table with 4 columbs and 9 rows. The 9 rows are 9 records in a returned recor set. I need to advance the recordset pointer after each </tr>

 

so the

 

while ($row = mysql_fetch_array($rsDraft))

{

}

 

is not going to work here. I need a kind of MSQL Move,next.  Any ideas on this.

 

Desmond.

 

Link to comment
Share on other sites

OK I am writing a website for a pub. For sake of arguments 10 beers and 10 records in the database.

Each record has Name of beer , price and ABV.

I have a table of 3 x 10 text boxes. I populate the first row of text boxes with the first row in the recordset. I then advance to the next row and repeat the exsersise. Their will always be 3 x 10.

 

Microsoft SQLserver has a record.movenext that places the cursor on the next row. so I need

 

<tr><td></td><td></td><td></td><td></td></tr>

move next

<tr><td></td><td></td><td></td><td></td></tr>

move next

<tr><td></td><td></td><td></td><td></td></tr>

 

does this help.

 

 

Link to comment
Share on other sites

<?php
  $rsDraft = mysql_query($sql);
  echo '<table>';
  while ($row = mysql_fetch_array($rsDraft)){
    echo "<tr><td>{$row['Name']}</td><td><td>{$row['Price']}</td><td>{$row['ABV']}</td></tr>";
  }
  echo '</table>';
?>

Link to comment
Share on other sites

No this will not work. All that you are doing is putting the database contents into a table and using the while ($row = mysql_fetch_array($rsDraft).

The data has to be put into a textbox that is in the table. Every textbox has to have its own name. This is a content managment system and the next page will update the database. That is why I need to iterate through the database.

 

would something like this work???

 

$SQL = "SELECT * FROM Drinks";

$rsDraft = mysql_query($SQL);

 

$row = mysql_fetch_array($rsDraft) ?>

<td><input type="text" Name="N1" value = <?= $row["Beer_Name"] ?></td>

 

<? $row = mysql_fetch_array($rsDraft) ?>

<td><input type="text" Name="N2" value = <?= $row["Beer_Name"] ?></td>

 

 

 

 

 

 

 

Link to comment
Share on other sites

Yes, but there is an easier way. I assume each entry has a unique id? I'm going to assume the column is called ID.

 

<?php
  $rsDraft = mysql_query($sql);
  echo '<table>';
  while ($row = mysql_fetch_array($rsDraft)){
    echo '<tr>';
    echo '<td><input type="text" name="name['.$row['ID'].']" value="'.htmlspecialchars($row['Name']).'" /></td>';
    echo '<td><input type="text" name="price['.$row['ID'].']" value="'.htmlspecialchars($row['Price']).'" /></td>';
    echo '<td><input type="text" name="abv['.$row['ID'].']" value="'.htmlspecialchars($row['ABV']).'" /></td>';
    echo '</tr>';
  }
  echo '</table>';
?>

 

On the page it posts to, do a print_r($_POST) and I think you'll be happy to see how cleanly it's organized :)

Link to comment
Share on other sites

OK thanks everyone for all your help. I now have 2 routes to take. One easy problem is how I convert the aray produced by. print_r($_POST);

 

I have Array ( [N1] => My Drink [P1] => Ni1 [H1] => Ni82 [ABV1] => 7.9 [N2] => My Drink2

 

How to I read this as Name , Value. I would not know the names of the text boxes or the size of the array.

Link to comment
Share on other sites

That isn't how I told you to set it up :)

 

You could go that route, where N#,P#,H#,ABV# are the fields, and # changes for each drink, but there is a better way. Do you have an ID for each drink in your table? If not, add one. It should be an INT with auto_increment and it should also be your primary key.

 

After that, if you use the code I posted before, your HTML will end up looking like this (I took out all the table tags just to show my point):

<input type="text" name="N[1]" value="My Drink" />
<input type="text" name="P[1]" value="Ni1" />
<input type="text" name="H[1]" value="Ni82" />
<input type="text" name="ABV[1]" value="7.9" />

<input type="text" name="N[2]" value="My Drink2" />
<input type="text" name="P[1]" value="Ni5" />
<input type="text" name="H[1]" value="Ni24" />
<input type="text" name="ABV[1]" value="6.2" />

The name syntax, where you reuse the name but change the value inside the square brackets will produce 4 arrays in $_POST:

Array (
  [N] => array( [1] => My Drink, [2] => My Drink2 )
  [P] => array( [1] => Ni1, [2] => Ni5 )
  [H] => array( [1] => Ni82, [2] => Ni24 )
  [ABV] => array( [1] => 7.9, [2] => 6.2 )
)

The code would then be:

<?php
  foreach($_POST['N'] as $id => $name){
    $price = $_POST['P'][$n];
    $h = $_POST['H'][$n];
    $abv = $_POST['ABV'][$n];
    //MySQL UPDATE command here
    //In the update, use SET with each of the 4 values, and a WHERE id = $id
  }
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.