...

View Full Version : help required which pulling out db info and dislaying correctly with php?



LJackson
09-15-2011, 06:55 PM
Hi All

I have some php code which pulls out codes from my db, now these codes come from several stores ie

code1->store1
code2->store2
code3->store2
code4->store3
code5->store4
etc....

and im trying to display them on my site so that the store is displayed with all its codes below

like so

store1
code1

store2
code2
code3

etc...

but all i am able to do at the mo is just echo out all the codes one after the other and not have them grouped by store?

here is an example of my query

SELECT * FROM stores AS stores LEFT JOIN ( tbl_codes AS codes ) ON stores.storeID = codes.storeID WHERE codes.codeID <> 'NULL' && UPPER( SUBSTRING( stores.storeName, 1, 1 ) = 'P' )ORDER BY stores.storeName ASC

and my php code

if(mysql_num_rows($query) > 0)
{
while ($row = mysql_fetch_array($query))
{
echo "<h1>".$row['storeName']."</h1>";
$shortCode_Desc = preg_replace("/.{0}(Valid | Expires | - expires | - Expires | - Valid ).*/", "", $row['details']) . "\n";
$timestamp = strtotime($row['expireDate']);
$format = '%d Days, %h Hours, %m Minutes';?>

<div class="discountcode">
<div>
<span class="usecodeBtn">
<a id="usecode" href="<?php echo htmlspecialchars($row['codeLink'])?>" target="_blank" title="Use Code <?php echo $row['code']?>">
<span>Use Code <?php echo $row['code']?></span>
</a>
</span>
<div class="codeDetails">
<span class="codeID"><?php echo $row['code']?>:</span>
<?php echo $row['details']."<br /><span style='color:#FF0000'>Exipres in: <b>".findTime($timestamp, $format)."</b></span>"?>
</div>
</div>
</div>
<?php
$count++;
}
}


the above codes outputs the following

store1
code1

store2
code2

store2
code3

instead of

store1
code1

store2
code2
code3


do i need to ammend my query or is it in the code?

i tried to do a foreach($row['storeName) after the while loop but it threw up an error.

many thanks
Luke

mlseim
09-15-2011, 11:02 PM
Try this ... untested ...




if(mysql_num_rows($query) > 0)
{
while ($row = mysql_fetch_array($query))
{
if($row['storeName']==$temp){
// do nothing
}
else{
echo "<h1>".$row['storeName']."</h1>";
$temp=$row['storeName'];
}
$shortCode_Desc = preg_replace("/.{0}(Valid | Expires | - expires | - Expires | - Valid ).*/", "", $row['details']) . "\n";
$timestamp = strtotime($row['expireDate']);
$format = '%d Days, %h Hours, %m Minutes';?>

<div class="discountcode">
<div>
<span class="usecodeBtn">
<a id="usecode" href="<?php echo htmlspecialchars($row['codeLink'])?>" target="_blank" title="Use Code <?php echo $row['code']?>">
<span>Use Code <?php echo $row['code']?></span>
</a>
</span>
<div class="codeDetails">
<span class="codeID"><?php echo $row['code']?>:</span>
<?php echo $row['details']."<br /><span style='color:#FF0000'>Exipres in: <b>".findTime($timestamp, $format)."</b></span>"?>
</div>
</div>
</div>
<?php
$count++;
}
}





.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum