Jump to content

Recommended Posts

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>";

 

 

Link to comment
https://forums.phpfreaks.com/topic/184012-database-and-loop-trouble/
Share on other sites

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.

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.