Array Formating and Display Error

Not open for further replies.
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';
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.
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
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
$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.
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|";
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.
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?
Check my previous post if you haven't already, I've made some additions to it.

but will this cause a problem, how will the loop know when the string is finished?
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.

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");
Not open for further replies.
Award-winning Mac antivirus and Internet security software