Page 1 of 1

problem with mysql while loop

Posted: Fri Jan 13, 2006 12:08 pm
by will83
Hi there,

The first while loop is working fine, however the second one does not work.

Can anyone see a problem??

Code: Select all

$query = "SELECT * from company_ins, company_log WHERE company_ins.ins_id = '$typeid'
			AND company_log.Id = company_ins.company_Id order by company_log.name";

$result = mysql_query($query, $conn);			

//check for any featured providers
while ($row = mysql_fetch_array($result, $conn)) {

if ($row['type_featured'] == "f") {
?>
<div class="linkbox">
		FEATURED<a href="<?php echo $row['link']; ?>" class="link" target="_blank"><strong><?php echo $row['name']; ?></strong></a>
	<p><?php echo $row['description']; ?><p><a href="<?php echo $row['link']; ?>" target="_blank"><?php echo $row['link']; ?></a></p>
</div>
<?php 
	}
}



while ($row = mysql_fetch_array($result, $conn)) {

if ($row['type_featured'] !='f') {
	
?>
<div class="linkbox">
		<a href="<?php echo $row['link']; ?>" class="link" target="_blank"><strong><?php echo $row['name']; ?></strong></a>
	<p><?php echo $row['description']; ?><p><a href="<?php echo $row['link']; ?>" target="_blank"><?php echo $row['link']; ?></a></p>
</div>
<?php 
	}
}
 ?>


Any ideas would be great, thanks

Will

Posted: Fri Jan 13, 2006 12:33 pm
by RobertGonzalez
How about comparing for type_featured inside of one while loop?

Code: Select all

$query = "SELECT * 
    FROM company_ins, company_log 
    WHERE company_ins.ins_id = '$typeid'
    AND company_log.Id = company_ins.company_Id 
    ORDER BY company_log.name";

$result = mysql_query($query, $conn);            

//check for any featured providers
while ($row = mysql_fetch_array($result, $conn)) {
     if ($row['type_featured'] == "f") {
?>
<div class="linkbox">
        FEATURED<a href="<?php echo $row['link']; ?>" class="link" target="_blank"><strong><?php echo $row['name']; ?></strong></a>
    <p><?php echo $row['description']; ?><p><a href="<?php echo $row['link']; ?>" target="_blank"><?php echo $row['link']; ?></a></p>
</div>
<?php
    }
    else
    {
?>
<div class="linkbox">
        <a href="<?php echo $row['link']; ?>" class="link" target="_blank"><strong><?php echo $row['name']; ?></strong></a>
    <p><?php echo $row['description']; ?><p><a href="<?php echo $row['link']; ?>" target="_blank"><?php echo $row['link']; ?></a></p>
</div>
<?php
    }
}
?>

Posted: Sat Jan 14, 2006 5:27 am
by will83
Yeh, I was thinking how I could get round that, but all the type_featured entries have to be printed first you see.

Thanks for the suggestion anyway.

Will

Posted: Sat Jan 14, 2006 8:01 am
by raghavan20
Please check here for mysql_fetch_array() syntax

Posted: Sat Jan 14, 2006 1:05 pm
by RobertGonzalez
will83 wrote:Yeh, I was thinking how I could get round that, but all the type_featured entries have to be printed first you see.
What do you mean "printed"? Do mean sent to the browser? That is easy enough to do using an intermediate step like reading $row into a new array and capturing the 'type_featured' value into a var using your while loop.

Posted: Sun Jan 15, 2006 9:26 am
by Bill H
In order to pass through the array a second time you must set the pointer back to the beginning

Code: Select all

mysql_data_seek ( $result,0 )