I want to display a multiple records from the database, 1 of the field needs to be displayed in a list format.

i have managed to achieve this, the problem is that the list is duplicating and incrementing into the next record.

Displays like this:

Record 1: a, b
Record 2: a, b, c, d
Record 3: a, b, c, d, e, f

the record actual contains in the database and should display:
Record 1: a, b
Record 2: c, d
Record 3: e, f

while($row = mysql_fetch_array($query)){ 
    
	$hf_id = $row["hf_id"]; 
	$title = $row["title"];
        $points =  explode(",", $row["points"]);
		foreach($points as $item) {
		$list .= "<li>$item</li>";
		}
 }

Thanks in Advance

Recommended Answers

All 10 Replies

Member Avatar for diafol

You keep concatenating from the previous record. Reset the $list at the top of the loop perhaps. You don't show your complete code - so it's difficult to see what you're doing 'downstream'.

You keep concatenating from the previous record. Reset the $list at the top of the loop perhaps. You don't show your complete code - so it's difficult to see what you're doing 'downstream'.

I just store the content of the file loops within a appended variable

$featured .= '<div style="height:105px; width:190px; background-color: #FFF; padding-left:10px; padding-top:10px; font-size: 10px;">' . $list . '</div>';
Member Avatar for diafol

Because this is outside the loop, $list will contain the whole list of all rows. Show the whole code. This piecemeal addition of single lines isn't really helping

I just tried to explain what i intepreted from your statement...
If you have some other problem then you are expected to give complete code so that we can help you to understand it's working...

$i=0;
while($row = mysql_fetch_array($query)){ 
 
	$hf_id = $row["hf_id"]; 
	$title = $row["title"];
        $points =  explode(",", $row["points"],$i);
		foreach($points as $item) {
		$list .= "<li>$item</li>";
		}
               $i=$i+2;
 }

Because this is outside the loop, $list will contain the whole list of all rows. Show the whole code. This piecemeal addition of single lines isn't really helping

@ IIM - I Tried what u suggested, didn't get the desired effect

I have included the php script and some html where relevant as its allot to copy and paste.

<?php $sqlCommand = "SELECT * FROM homefeatured WHERE active = '1' ORDER BY 'displayorder' ASC LIMIT 6"; 
// Execute the query here now 
$query = mysql_query($sqlCommand) or die (mysql_error()); 

$featured = '';
while($row = mysql_fetch_array($query)){ 
    
	$hf_id = $row["hf_id"]; 
	$title = $row["title"]; 
	$image = $row["image"];
	$link = $row["link"];
	$points =  explode(",", $row["points"]);
		foreach($points as $item) {
		$list .= "<li>$item</li>";
		}
	
$featured .= ' <div style="height:275px; float: left; margin-top:7px; background-color: #FFF; margin-left:15px;">
<table border="0">
  <tr>
    <td><div style="height:115px; width:200px;"><a href="http://'. $link . '">' . $featured_pic . '</a></div> </td>
  </tr>
  <tr>
    <td><div style="height:20px; text-align: center; background-color: #000; color: #FFF; overflow: hidden; width:200px;"><a style="color:#FFF; font-size: 12px; text-decoration: none;
" href="http:// '. $link . '">' . $title . '</a></div></td>
  </tr>
  <tr>
    <td><div style="height:105px; width:190px; background-color: #FFF; padding-left:10px; padding-top:10px; font-size: 10px;">' . $list . '</div></td>
  </tr>
</table>		   
</div>';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Premier Events - Homepage</title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="spotlight"><?php print "$featured"; ?></div>
</body>
</html>

can you tell me the output you got when you used my code????
Sp that i can know how is it working...

can you tell me the output you got when you used my code????
Sp that i can know how is it working...

The first duplication is still occurring

The first step loop group all the points into 1 list

Record 1

  • a b

Record 2

  • a b
  • c
  • d

Record 3

  • a b
  • c
  • d
  • e
  • f

Record 4

  • a b
  • c
  • d
  • e
  • f
  • g
  • h

hey there is also an error to use <li>in loop
it will just show each output with bullets...
and for getting output as
a,b
c,d
d,e
use substring.....hope it will suffice your requirement...

Member Avatar for diafol

Reset the $list at the top of the loop perhaps.

Did you try it?

while($row = mysql_fetch_array($query)){ 
        $list = "";
	$hf_id = $row["hf_id"];

Did you try it?

while($row = mysql_fetch_array($query)){ 
        $list = "";
	$hf_id = $row["hf_id"];

Such a simple fix

Thanks Ardav.

I shall mark it as solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.