Jump to content

trouble with looping through database and displaying correct output


darga333

Recommended Posts

Hi there! I am new to PHP and am having trouble with this loop. For some reason, when everything is outputted... it works perfectly if there is only 1 record that is returned in the $sql_1 statement... The problem is when the $sql_1 statement returns more than one record, it displays the $discount_message twice. The real problem is that $discount_message already contains both the discounts it found.. so there is no need to display both discounts again. Please help!!

[code]
    $sql_0  = " SELECT  `user_id`,`sRepresentatives` FROM  `ucfmembers`,`ucfdiscount` WHERE ucfmembers.user_id=ucfdiscount.iMemberId AND ucfmembers.bBusiness =1 AND ucfdiscount.sActive=1 AND ucfdiscount.sFeatured=1 ORDER  BY ucfmembers.user_id ASC ";
    $result_0 = mysql_query($sql_0);
    while($row_0 = mysql_fetch_array($result_0))  {
    $discount_message = "";
    $user_id = $row_0['user_id'];
    $rep = $row_0['sRepresentatives'];
    
    
        $sql_1  = " SELECT *,ucfdiscountcategory.sName,ucfdiscount.sDescription FROM `ucfdiscount`,ucfdiscountcategory WHERE ucfdiscount.sActive = 1 AND ucfdiscount.sFeatured = 1 AND ucfdiscount.iDisCategoryId = ucfdiscountcategory.idiscategoryid AND ucfdiscount.iMemberId=$user_id";
                
        $result_1 = mysql_query($sql_1);
            
            

            if (mysql_num_rows($result_1) > 0) {
            while($row_1 = mysql_fetch_array($result_1))  {
                
                
                //$current_catid = $row_1['iDisCategoryId'];
                $discount_message .= "<h2>".$row_1['sName']."</h2>";
                $discount_message .= "<p><strong>".$row_1['sBusinessName']."</strong><br>\n";
                $discount_message .= $row_1['sHeadlines']."<br>\n";
                $discount_message .= $row_1[1]."<br>\n";
                $discount_message .= $row_1['sDescription']."<br>\n";
                $discount_message .= $row_1['sPhone']."<br>\n";
                $discount_message .= "<a href=\"http://www.ucflife.com/viewcoupon.php?id=".$row_1['idiscountid']."\">View your coupon!</a>";
                //echo $current_catid;
                
            }
            
    }
    echo "<br />This is discount message OUTSIDE OF THE LOOP: ".$discount_message."<br /><br />";
            echo $rep;
    }

    
    echo "<h1>You are Done!</h1>";
[/code]
Link to comment
Share on other sites

Im sorry but I dont understand what you are saying. I tried commenting out that while loop, but then it doesnt display anything. If i remove the code inside that while loop, then the discounts arent displayed on the page.

The first query works fine. It selects all the user_id's that i need. Then the second query is suposed to get all the discounts for that user_id and print them all on the screen.

I think I have something seriously messed up with my code because when it finds more than 1 discount for a user, it displays all the discounts but multiple times. For example. If there is 2 discunts, it will display 2 discounts, but twice. I have no idea what I am doing. Could you please be more descriptive on what I need to do. I am very much new to programming.
Link to comment
Share on other sites

Oh, sorry, I think I overlooked things.
Your script seems to be fine, but what is it displaying? Only the discount multiples times ("This is discount message OUTSIDE OF THE LOOP: ".$discount_message."") or $rep is also "affected"?

And for someone new to programming you are actually doing great.
Link to comment
Share on other sites

Hey! I just figured it out, you have to make the very first SQL statement a GROUP BY instead of an ORDER BY... That took over 8 hours of guessing and checking to figure out.

I really want to be a PHP guru and a javascript guru.. i am just a beginner.. but thanks for the compliment! the hardest things to me are loops, for each loops, and any other loop you can think of, they are a pain in the ass.

and oh yeah that error that says something like not a valid mysql resource, that one is truely a pain!
some stuff i just dont understand about coding yet but I appreciate the help!

ohhh hey!! maybe you might know something about this comma thing.. whenver you try to update data if the data contains one or more commas... oh boy a huge problem.. every single time mysql stops on the comma and dies.. i had to fix a good 100 things by hand.. .. i would run the script, it would die, then i would get the id of where it died.. then i would find all the commas,.. then i would put a \ before the comma.. then run again... then dies... repeat.. lol

there has got to be a way to run a query to get all of the information even with commas...

or run a query then get all of the data.. then write a snipplet of code to put a \ before each comma, so mysql can be happy...

you know anything about this?
Link to comment
Share on other sites

Well, you should first add qoutes to the values, I mean, like this:

UPDATE `table` SET field = 'value1', field2 = 'value2' .... etc.

To add backslashes use mysql_real_escape_string():
[a href=\"http://www.php.net/manual/en/function.mysql-real-escape-string.php\" target=\"_blank\"]http://www.php.net/manual/en/function.mysq...cape-string.php[/a]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.