otuatail Posted May 21, 2008 Share Posted May 21, 2008 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. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted May 21, 2008 Share Posted May 21, 2008 What do you mean by "I need to advance the recordset pointer after each </tr>"? Quote Link to comment Share on other sites More sharing options...
otuatail Posted May 21, 2008 Author Share Posted May 21, 2008 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. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted May 21, 2008 Share Posted May 21, 2008 <?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>'; ?> Quote Link to comment Share on other sites More sharing options...
otuatail Posted May 21, 2008 Author Share Posted May 21, 2008 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> Quote Link to comment Share on other sites More sharing options...
rhodesa Posted May 21, 2008 Share Posted May 21, 2008 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 Quote Link to comment Share on other sites More sharing options...
otuatail Posted May 21, 2008 Author Share Posted May 21, 2008 Ok thanks for that I will try the print_r($_POST) But would each $row = mysql_fetch_array($rsDraft) take me to the next row or to the same one. Desmond. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted May 22, 2008 Share Posted May 22, 2008 it gets the next row...that while syntax is the standard and most commonly used method Quote Link to comment Share on other sites More sharing options...
otuatail Posted May 22, 2008 Author Share Posted May 22, 2008 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. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted May 22, 2008 Share Posted May 22, 2008 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 } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.