AL123 Posted December 4, 2009 Share Posted December 4, 2009 I have integrated an ajax script from w3schoolsand it does everything it should. But the output is double. I used a foreach to loop through a row and print a table. The table prints twice. The original script used another db connection and output the values with a while loop, but I could not get their loop to work. here is a link to the site: http://nuke.empireindustry.com/Blogspiracy/ Can anyone help? Thanks, AL123 <?php $r=$_GET["r"]; $dbh=new PDO('mysql:host=localhost;dbname=///','///','///'); if (!$dbh) { die('Could not connect: ' . mysql_error()); } function get_quote($r) { global $dbh; $sql="SELECT name, quote FROM tblQuote WHERE id = '".$r."'"; $sth = $dbh->prepare($sql); $sth->bindValue(':r', $r, PDO::PARAM_INT); $sth-> execute(); $row = $sth->fetch(PDO::FETCH_ASSOC); return $row; } $display = get_quote($r); //print_r($display); echo "<table border='1'> <tr> <th>name</th> <th>quote</th> </tr>"; //while($row = mysql_fetch_array($display)) //$row = mysql_fetch_array($result) //foreach($display as $value) foreach($display as $key => $value) { echo "<tr>"; echo "<td>" . $display['name'] . "</td>"; echo "<td>" . $display['quote'] . "</td>"; echo "</tr>"; } echo "</table>"; Quote Link to comment https://forums.phpfreaks.com/topic/184012-database-and-loop-trouble/ Share on other sites More sharing options...
premiso Posted December 4, 2009 Share Posted December 4, 2009 Please use to display code. Your issue lies here: foreach($display as $key => $value) { echo "<tr>"; echo "<td>" . $display['name'] . "</td>"; echo "<td>" . $display['quote'] . "</td>"; echo "</tr>"; } You are using $display to call the items, if $display is not a multi-dimmensional array then you just need to display the items, if it is you would call it this way: foreach($display as $value) { echo "<tr>"; echo "<td>" . $value['name'] . "</td>"; echo "<td>" . $value['quote'] . "</td>"; echo "</tr>"; } I am not sure which it is or is not, but if it is not a multi-dimmensional array the reason you were getting the results doubled is that you have 2 items in the array, name and quote, so foreach runs for each of those items in the array and will display it. So if that is the case and it will never be a multi-dimmensional array perhaps this is what you want: echo "<tr>"; foreach($display as $value) { echo "<td>" . $value . "</td>"; } echo "</tr>"; Not really sure, but there are a few options for you to look over and ask questions about / try. Quote Link to comment https://forums.phpfreaks.com/topic/184012-database-and-loop-trouble/#findComment-971484 Share on other sites More sharing options...
AL123 Posted December 4, 2009 Author Share Posted December 4, 2009 The second option worked perfect. Thanks a lot! AL123 echo "<tr>"; foreach($display as $value) { echo "<td>" . $value . "</td>"; } echo "</tr>"; Quote Link to comment https://forums.phpfreaks.com/topic/184012-database-and-loop-trouble/#findComment-971495 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.