Discover Habbo's history
Treat yourself with a Secret Santa gift.... of a random Wiki page for you to start exploring Habbo's history!
Happy holidays!
Celebrate with us at Habbox on the hotel, on our Forum and right here!
Join Habbox!
One of us! One of us! Click here to see the roles you could take as part of the Habbox community!


Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2011
    Location
    Melbourne
    Posts
    637
    Tokens
    1,376

    Default What's wrong with this code? [php] +reps

    Hey everyone.
    I'm having problems with my scrappy code. It only serves as a purpose to show me the recipes that I have acquired.. Only problem is it is showing just the first ingredient.. Doesn't even show me the name of the recipe??

    Here's the code I have written up:

    PHP Code:
       <?php
        $recipe 
    "Coffee and Date Dough"// this isn't normally hard coded..
        
    echo('<table width="25%" border="1">');

        
    // This basically gets all of the recipe into an array
        
    $query mysql_query("SELECT * FROM recipe WHERE recipe='{$recipe}'");
        
    $count 1;
        while(
    $row mysql_fetch_array($query)){
            
    // Couldn't think of a better way to count to 18 without using something like this.
            
    $ing "ing".$count;
            
    $per "per".$count;

            
    // Echo out the table information
            
    echo("<tr><td>$row[$ing]</td><td>$row[$per]</td></tr>");
            
    $count++;
        }
        echo(
    "</table>");
       
    ?>
    and here is a sample sql


    @robbie! ?

  2. #2
    Join Date
    May 2007
    Posts
    10,481
    Tokens
    3,140

    Latest Awards:

    Default

    I think that a serialised array would have been a better option that 20 columns..

    Also I think it only iterates the while loop once as once you call mysql_fetch_array($query) it moves to the next mysql row of results, since theres' only one row after the first call of the function it's equal to 0 which terminates the loop.
    Chippiewill.


  3. #3
    Join Date
    Oct 2011
    Location
    Melbourne
    Posts
    637
    Tokens
    1,376

    Default

    ah okay. I'm a bit confuzzled now. How would I go about making it show in a table? It's mainly for use on mobile phones (since if I'm at work it'd be easiest to look at it there)..

    I mean, I don't REALLY want to have to write heaps of code just to spit all the information out but if i have to, i have to...

  4. #4
    Join Date
    Jul 2004
    Location
    California
    Posts
    8,725
    Tokens
    3,789
    Habbo
    HotelUser

    Latest Awards:

    Default

    Without looking to see if your mysql methods are being called correctly and without looking at your sql table at all, immediately I can see that the reason (at least) that you're only being displayed the first bit of data is because you never actually increment $count in the correct place. You will need to do (something) like this:

    http://pastebin.com/kQSGAZJb

    For every iteration of your while loop, you're iterating through the different rows of your sql table, but I'm just going to assume that every row has a variable called ing1, ing2, ing3 and per1, per2 and per3 etc etc. so you could either write out a bunch of lines to accommodate for each of these variables inside your while loop, or you could use a for loop (something similar as above to suit your needs), to build the keys for your associative array and print out their values accordingly inside that while loop.
    Last edited by HotelUser; 27-06-2012 at 01:07 AM. Reason: put source on pastebin because fckeditor is horrible
    I'm not crazy, ask my toaster.

  5. #5
    Join Date
    Oct 2007
    Posts
    824
    Tokens
    71

    Latest Awards:

    Default

    Why do you store each ingredient in it's own row resulting in several unecessary rows? Why not create one ingredients row and store them separated by some character? Ex: "1 cup milk|2 cups butter|1 cup flour". Then when you need to display them, use implode() and a loop
    Vouches
    [x][x]

  6. #6
    Join Date
    Oct 2011
    Location
    Melbourne
    Posts
    637
    Tokens
    1,376

    Default

    Quote Originally Posted by Trigs View Post
    Why do you store each ingredient in it's own row resulting in several unecessary rows? Why not create one ingredients row and store them separated by some character? Ex: "1 cup milk|2 cups butter|1 cup flour". Then when you need to display them, use implode() and a loop
    Because I grabbed the ingredients from another file which doesn't display in firefox/chrome, only IE. Might do it your way. Unless someone knows how/why a sample file like this doesn't work? (worst.coding.everrrrrr!)

    http://pastebin.com/KsDuMScn

  7. #7
    Join Date
    May 2007
    Posts
    10,481
    Tokens
    3,140

    Latest Awards:

    Default

    Quote Originally Posted by Trigs View Post
    Why do you store each ingredient in it's own row resulting in several unecessary rows? Why not create one ingredients row and store them separated by some character? Ex: "1 cup milk|2 cups butter|1 cup flour". Then when you need to display them, use implode() and a loop
    He should be serialising an array to do this instead. For readability he should serialise them with JSON.

    json_encode() before they go into the array and json_decode() as they come out.

    Also I believe with your method you need to explode() the string not implode().

    Edit:
    Last edited by HotelUser; Today at 02:07 AM. Reason: put source on pastebin because fckeditor is horrible
    Disable the rich text editor in the control panel, it's not too great for any kind of use.
    Last edited by Chippiewill; 27-06-2012 at 08:54 AM.
    Chippiewill.


  8. #8
    Join Date
    Jul 2004
    Location
    California
    Posts
    8,725
    Tokens
    3,789
    Habbo
    HotelUser

    Latest Awards:

    Default

    Quote Originally Posted by Chippiewill View Post
    He should be serialising an array to do this instead. For readability he should serialise them with JSON.

    json_encode() before they go into the array and json_decode() as they come out.

    Also I believe with your method you need to explode() the string not implode().

    Edit:

    Disable the rich text editor in the control panel, it's not too great for any kind of use.
    The rich text editor also doesn't seem to play nice with the iPad either, as aforementioned vBulletin's implementation of CKeditor is horrifying

    The OP could have taken a serialized approach here, or as someone else mentioned just used CSV using a delimiter of | (using implode to store and explode to display), although that's not the immediate bug in his code causing it to not function. Regardless of his technique in storing data serialized or largely in SQL, his error was in how he iterated through his data per row of content. Perhaps once the OP has validated that his current model is functional, he could read up on CSV and serialization methods of PHP, because I know at least the latter is well documented on the PHP reference manual
    I'm not crazy, ask my toaster.

  9. #9
    Join Date
    Oct 2011
    Location
    Melbourne
    Posts
    637
    Tokens
    1,376

    Default

    Quote Originally Posted by HotelUser View Post
    The rich text editor also doesn't seem to play nice with the iPad either, as aforementioned vBulletin's implementation of CKeditor is horrifying

    The OP could have taken a serialized approach here, or as someone else mentioned just used CSV using a delimiter of | (using implode to store and explode to display), although that's not the immediate bug in his code causing it to not function. Regardless of his technique in storing data serialized or largely in SQL, his error was in how he iterated through his data per row of content. Perhaps once the OP has validated that his current model is functional, he could read up on CSV and serialization methods of PHP, because I know at least the latter is well documented on the PHP reference manual
    I just copied your method. If I ever get time I'll suss that stuff out. I've been busy/tired from work recently though!

Posting Permissions

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