problem with mysql while loop

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
will83
Forum Commoner
Posts: 53
Joined: Thu Nov 10, 2005 3:13 pm

problem with mysql while loop

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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
    }
}
?>
User avatar
will83
Forum Commoner
Posts: 53
Joined: Thu Nov 10, 2005 3:13 pm

Post 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
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

Please check here for mysql_fetch_array() syntax
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

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