repeat repeat repeat

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sonicfusion
Forum Newbie
Posts: 1
Joined: Sat Jan 04, 2003 8:34 am

repeat repeat repeat

Post 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
User avatar
PaTTeR
Forum Commoner
Posts: 56
Joined: Wed Jul 10, 2002 7:39 am
Location: Bulgaria
Contact:

Post 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
f1nutter
Forum Contributor
Posts: 125
Joined: Wed Jun 05, 2002 12:08 pm
Location: London

Re: repeat repeat repeat

Post 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>");  
}
?>
DaiWelsh
Forum Commoner
Posts: 36
Joined: Wed Jan 08, 2003 9:39 am
Location: Derbyshire, UK

Post 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
Post Reply