Array Formating and Display Error

Discussion in 'Coding Help' started by ziycon, Sep 28, 2008.

  1. ziycon

    ziycon New Member

    I have this function below and it takes in the id of the article and the table its from in the database.
    The first query will return a string like so ;1|;4|;7|;8| which is assigned to $format_array, what im trying to do in the second query and loop is to check the id returned from each row of review_format table and if its equal to one of the numbers in the $format_array that it will echo out to screen "checked".
    function display_edit_format($id,$table) {
        $sql = mysql_query("SELECT format FROM ".$table." WHERE id=".$id."");
        while ($row = mysql_fetch_array($sql)) 
            $i = 0;
            $format_array = $row['format'];
            $array_size = sizeof($format_array);
            $sql = mysql_query("SELECT id,description FROM review_formats");
            while ($row = mysql_fetch_array($sql)) 
                echo'<input type="checkbox" name="format[]" value=";'.$row['id'].'|" class="form"';
                    for($i = 0;$i < $array_size;$i++)
                        if($row['id'] == $format_array[$i])
                            echo' checked';

  2. Briask

    Briask New Member

    What is the error you are getting?
  3. ziycon

    ziycon New Member

    Its not displaying an error, its just always ticking every tick box outputted!
  4. louie

    louie New Member

    The code doesn't look right to me at all. can you explain in more detail what exactly is suppose to happen?
  5. Mmm, it does look a bit strange.

    Just looking at the loop... before entering the outer while loop, you set $sql to one query, then inside the loop you set it as something else (in preperation for the inner while loop), then when the outer loop executes again, the $sql used in the outer while loop isn't reset to what it was when it executed the first time.
    I could be wrong, but I think your re-use of variable names is really confusing.
  6. louie

    louie New Member

    re-using the variables is not confusing but resets the ones set above.
    change $sql & $row to something else like $sql_x & respectively $row_x
  7. ziycon

    ziycon New Member

    Hope this makes it clearer, i noticed the code was wrong before, maybe this is more correct.

    Ok, the first query will return a string like ;1|4;|6;|;7| this string is stored in the variable $format_array which is based on the id and table passed in as parameters.
    Then the next SQL query will return a list of names and if the id of one of the names matchs one of the number returned in the first SQL query the the 'input' line for the tick box will be checked.

    I know i have to take the string ;1|4;|6;|;7| and put it into proper array elements but i'm unsure of where to do this!?

    Code is at:
    pastebin - collaborative debugging tool
  8. $format_array = $row1['format'];

    ^ What exactly is going into $format_array at that point?
    I think a print_r of $format_array would be handy to see wtf it's doing.

    You could simplify this a whole lot if you (temporarily) construct some vars in php and fill them with the data you're getting from the database... get rid of a few unknowns and give us a complete, self-contained version.
  9. ziycon

    ziycon New Member

    Below is basically whats happening there, i'm unsure how to take that string and ad it to an array, like it should remove ; and | so there would be 4 array elements which would hold the values 1,4,6 and 7. The string is always going to be like the below with the ; before the value and an | after the value.

    $format_array = ";1|;4|;6|;7|";
  10. Ah right...
    Something like...
    $format_array = str_replace ("|", "", $format_array);
    $format_array = explode(";", $format_array);
    ...would break them up... although you might get some empty array elements, hmm... in which case you can add:
    To get rid of the first empty element.

    Although, if $format_array is actually an array to begin with and not just a string, then you'll have to do more.
    I'm assuming this...
    $format_array = ";1|;4|;6|;7|"; 100% true.

    Do a...
    ... just to be sure.

    If it's just a string, the output will be

    but if it's an array, then it'll be
    Array ( [0] => ;1|;4|;6|;7| )

    In which case you'll have to loop through the array and build a multidimensional array... or array of arrays if you like.
  11. ziycon

    ziycon New Member

    Is there a way to take say the first bit like ;1\ remove the ; and | symbols and then just at the value to the array element but will this cause a problem, how will the loop know when the string is finished?
  12. Check my previous post if you haven't already, I've made some additions to it.

    If you use explode, then there'll be no loop to worry about... you can use ; or | as cutting points... str_replace which ever one you're not going to use for explode.

    Then for the resulting array you can use foreach ... again, the loop will take care of itself.
  13. louie

    louie New Member

  14. ziycon

    ziycon New Member

    What does the question mark symbol do?
  15. louie

    louie New Member


    normally you write an if -> then -> end if statement like this:
     echo ....
    //but can also be written in just one line
    echo (iftrue ? (then"blah" : (else) "blah,blah");
  16. Briask

    Briask New Member

    It is called the Ternary operator, essentially a neat&quick way of doing and if-then-else see ?: - Wikipedia, the free encyclopedia
  17. ziycon

    ziycon New Member

    Nope, not working, doesn't give an error or anything, just displays all the tick boxes unticked!

    It needs to check every record id from the second SQL query against every element of $format_array, i can't see anywhere its doing that, maybe im just missing it!?
  18. louie

    louie New Member

  19. ziycon

    ziycon New Member

    $format_array is outputting this:
    Array ( [0] => )

    No, theres nowhere to see it as its in an admin section of the site.
  20. It's hard at the moment to visualize what's happening.

    Could you try something like this...
    pastebin - collaborative debugging tool
    ... to give us exactly what the db is returning for both queries?
    (Not sure if the code works, but you can see what I'm getting at.)
    Once the database is out of the equation, this'll get a lot more simple IMO.

Share This Page