Page 1 of 1

repeat repeat repeat

Posted: Sat Jan 04, 2003 8:34 am
by sonicfusion
right....
i have a problem... 1st lets look at the code...

news_inc.php
----------------------

Code: Select all

<?php
<? 
include('HIDDEN FOR SECURITY'); 
mysql_pconnect("$host","$user","$pass"); 
mysql_select_db("$base"); 
$news_inc_result = mysql_query("SELECT `type` FROM `tbl_sf_news` ORDER BY `type` ASC"); 
while($news_inc=mysql_fetch_array($news_inc_result
)) 
{ 
$type=$news_inc["type"]; 
include('inc/news_mini.php'); 
} 
?>
?>


news_mini.php
----------------------

Code: Select all

<?php
<? 
/* $type = ''; required before include*/ 
include('HIDDEN FOR SECURITY'); 
echo("$type:<br>"); 
mysql_pconnect("$host","$user","$pass"); 
mysql_select_db("$base"); 
$news_mini_result = mysql_query("SELECT `id`,`title` FROM `tbl_sf_news` WHERE 1 AND `type` LIKE '$type' ORDER BY `date` ASC LIMIT 0, 3"); 
while($news_mini=mysql_fetch_array($news_mini_resu
lt)) 
{ 
$id=$news_mini["id"]; 
$title=$news_mini["title"]; 
$date=$news_mini["date"]; 
echo("<a href='http://www.sonicfusion.co.uk/news.php?id=$id'>//$title - $date</a><br>"); 
} 
echo("<br>"); 
?> 
?>
right and now look at the results at http://www.sonicfusion.co.uk

why are the types repeting how can i make it so each type only appears once?

thanks in advance

p.s. replies in noobie language if possible please

Posted: Mon Jan 06, 2003 5:05 am
by PaTTeR
Try this

Code: Select all

......
while($news_inc=mysql_fetch_array($news_inc_result,MYSQL_ASSOC)) &#123;
.....
By using MYSQL_BOTH, you'll get an array with both associative and number indices. Using MYSQL_ASSOC, you only get associative indices (as mysql_fetch_assoc() works), using MYSQL_NUM, you only get number indices (as mysql_fetch_row() works).
for moore info and help look at http://php.net/mysql_fetch_array

Re: repeat repeat repeat

Posted: Mon Jan 06, 2003 5:13 am
by f1nutter
It is repeating because you have a loop within another loop. If you place the code from news_mini.php where you have included it, you will see the news_mini.php is inside the while loop from news_inc.php

You will probably be able to combine everything into one page, which will be more efficient. Try this?

Code: Select all

<?php
include('HIDDEN FOR SECURITY');
mysql_pconnect("$host","$user","$pass");
mysql_select_db("$base");
$news_inc_result = mysql_query("SELECT `id`, `title`, `type` FROM `tbl_sf_news` ORDER BY `type`, 'date' ASC LIMIT 0, 3");

while($news_inc = mysql_fetch_array($news_inc_result))
{  
 $type = $news_inc["type"];
 echo("$type:<br>");
 $id = $news_inc["id"];  
 $title = $news_inc["title"];  
 $date = $news_inc["date"];  
 echo("<a href='http://www.sonicfusion.co.uk/news.php?id=$id'>//$title - $date</a><br>");  
}
?>

Posted: Wed Jan 08, 2003 10:42 am
by DaiWelsh
I am guessing that there are multiple entries in the news table for each type, but you only want the type line displayed once for each type?

Therefore you should either do SELECT DISTINCT type in your first query to get just one row for each different type, or for greater efficiency do a single query like f1nutter suggests but with one modification

$last_type='';
while($news_inc = mysql_fetch_array($news_inc_result))
{
$type = $news_inc["type"];
if($type != $last_type)
{
echo("$type:<br>");
$last_type=$type;
}

This should ensure that the $type: line is only output when a new type starts. I am assuming here that type is char data from the context.

HTH,

Dai