Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,093
    Thanks
    297
    Thanked 12 Times in 12 Posts

    Question help required which pulling out db info and dislaying correctly with php?

    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
    PHP Code:
    SELECT FROM stores AS stores LEFT JOIN tbl_codes AS codes ON stores.storeID codes.storeID WHERE codes.codeID <> 'NULL' && UPPERSUBSTRINGstores.storeName1) = 'P' )ORDER BY stores.storeName ASC 
    and my php code
    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

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,456
    Thanks
    8
    Thanked 1,084 Times in 1,075 Posts
    Try this ... untested ...

    PHP Code:

    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
    ++;
            }
        }


    .


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •