Array Formating and Display Error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ziycon
    Contributor
    • Sep 2008
    • 384

    Array Formating and Display Error

    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".
    Code:
    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';
                        }
                    }
                echo'>'.$row['description'].'<br>';
            }
        }
    }
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    If your first query is returning all those ID's as a single string (which is a bad idea btw. See this article to see more), then you will have to split them up before you can loop through them as an array.

    Check out the explode function to do that.

    Comment

    Working...