Array Formating and Display Error

Status
Not open for further replies.

ziycon

New Member
Array1 is outputting:
Array ( [0] => Array ( [0] => ;4|;5|;6|;7|;8| [format] => ;4|;5|;6|;7|;8| ) )

Array2 is outputting:
Array ( [0] => Array ( [0] => 1 [id] => 1 ) [1] => Array ( [0] => 2 [id] => 2 ) [2] => Array ( [0] => 3 [id] => 3 ) [3] => Array ( [0] => 4 [id] => 4 ) [4] => Array ( [0] => 5 [id] => 5 ) [5] => Array ( [0] => 6 [id] => 6 ) [6] => Array ( [0] => 7 [id] => 7 ) [7] => Array ( [0] => 8 [id] => 8 ) )

This is what is displayed on screen, the first query is what is already 'checked' so as the second query displays all the options from the DB its should check the items relevant to the first query number if you get me!

 
D

Deleted member 444

Guest
Right, so ...

Array2 is all the checkboxes that exist in this context.
Array1 is which of those checkboxes are checked.

You want to use the data in Array2 to decide which checkboxes are created, and reference Array1 to see which ones need to be already checked.

Am I getting this right?
 
D

Deleted member 444

Guest
Ok, check this out...
PHP pastebin - collaborative debugging tool

I've reconstructed the arrays returned from the database query... so at the very least we have those to play around with.

I cleaned them both up.

Then combined them into a single array.

The code isn't complete because it assumes there'll be x number of checkboxes that'll always be numbered sequentially from 1 to x... where this mightn't be the case at all.
There are some ideas in there anyway, maybe you can adapt some of it.

Probably what you'll want to do is build a new array and have two keys in each element...
[0]=> (['checkbox_id'] => "PS3", ['checked']=>"TRUE")
[1]=> (['checkbox_id'] => "Xbox", ['checked']=>"FALSE")
...and so on.
 

ziycon

New Member
I'm trying to follow your code there, i've gotten most of it but im confused about the first bit where the first SQL query is passed in with the 'pre-ticked' options!??
 
D

Deleted member 444

Guest
Hmm, not sure which bit you mean... what's the line number?

If it's not what you're trying to achieve then ignore it... I only got the "wait... what if?" moment after I'd posted the code.

What I was too tired to do at the end is make a single array that'd be the size of how ever many checkboxes there are.
so you'd have an array with:
[box number] => ([box id], [checked or not])
[box number] => ([box id], [checked or not])
[box number] => ([box id], [checked or not])

So you know how many boxes there are, what each type is and whether it's checked or not. Then itirate through a loop and spew them out.
 

louie

New Member
When I ask you to give me the value of the array earlier you wrote Array ( [0] => )
which means that the sql returns no values, in turn all check-boxes are unchecked.

The first code you gave us, the second loop was included into the first loop, but then it was moved outside. Is that correct?

I think you got lost in so many versions and coding structure the reason it keeps failing to work.

This is your first code, which i change it a bit to clear the confusion:
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'];
        echo $format_array;//print value
        $array_size = sizeof($format_array);
        
        $sql2 = mysql_query("SELECT id,description FROM review_formats");
        while ($row2 = mysql_fetch_array($sql2))  {
            $x_value = $row2['id']; echo $x_value."<br />";//print value
               echo '<input type="checkbox" name="format[]" value=";'.$row2['id'].'|" class="form"';
                for($i = 0;$i < $array_size;$i++)
                {
                    if($row2['id'] == $format_array[$i])
                    {
                        echo' checked';
                    }
                }
            echo'>'.$row2['description'].'<br>';
        }
    }
}

Notice the lines that echo the values. Can you tell us what it prints out?
 

ziycon

New Member
Ok, i'm getting confused now, i'll explain where I'm at and what code I'm using and what its ment to do.

This is the code i have:
pastebin - collaborative debugging tool

This is what is displayed on screen. The first query will return a string like ;1|;4|;7| and astore it in $format_array. Now the second SQL query will run and will return the id number for multiple rows and create tick boxes for every row id, as the tick boxes are beign created it should check againest the $format_array elements and if one of the row ids from the second query matches any of the numbers from $format_array which is ;1|;4|;7| so thats 1,4 or 7 then it would create a tick box that is checked.
 

louie

New Member
give this code a go and please let us know what's happening:
Code:
function display_edit_format($id,$table) {
  $i = 0;
  $format_array = array();
  $array_size = 0;
  $sql1 = mysql_query("SELECT format FROM ".$table." WHERE id=".$id."");
  while ($row1 = mysql_fetch_array($sql1)){
    $i = 0;
    $format_array = $row1['format'];//this should return ;1|;2| etc.
    $array_size = sizeof($format_array);
  //}
   $sql2 = mysql_query("SELECT id,description FROM review_formats");
   while ($row2 = mysql_fetch_array($sql2)){
  $ck_box = ";".trim($row2['id'])."|";
  $description = $row2['description'];
      echo '<input type="checkbox" name="format[]" value="'.$ck_box.'" class="form"'.
  (in_array($ck_box,$format_array) ? ' checked="checked"' : '').
  '>'.$description.'<br />';
      //for($i = 0;$i < $array_size;$i++){
         //if($row2['id'] == $format_array[$i]){
            //echo' checked';
         //}
      //}
      //echo '>'.$row2['description'].'<br />';
   }//end second while
  }//end first while
}

also changed here: pastebin - collaborative debugging tool
 

ziycon

New Member
This is the output from that code, its comes up for every checkbox line:
Warning: in_array() [function.in-array]: Wrong datatype for second argument in C:Apachehtdocssys_configsql.php on line 1173
And $format_array prints this to screen:
I tried exploding the string into the array like so with your code louie:
pastebin - collaborative debugging tool

$array_size returns 1 and $format_array returns Array ( [0] => ).
 

louie

New Member
lets try a different way without the array
Code:
function display_edit_format($id,$table) {
  $i = 0;
  $format_array = "";
  $array_size = 0;
  $sql1 = mysql_query("SELECT format FROM ".$table." WHERE id=".$id."");
  while ($row1 = mysql_fetch_array($sql1)){
    $i = 0;
    $format_array = $row1['format'];//this should return ;1|;2| etc.
    //$array_size = sizeof($format_array);
  //}
   $sql2 = mysql_query("SELECT id,description FROM review_formats");
   while ($row2 = mysql_fetch_array($sql2)){
  $ck_box = ";".trim($row2['id'])."|";
  $description = $row2['description'];
      echo '<input type="checkbox" name="format[]" value="'.$ck_box.'" class="form"'.
  (stristr($format_array,$ck_box) ? ' checked="checked"' : '').
  '>'.$description.'<br />';
   }//end second while
  }//end first while
}

Let me know.
 

ziycon

New Member
That works fine on my test site and its ticking the right boxes on the live sit but wont save the new value to the DB for some reason!?!

Edit: Working fine, its was a different issue why it wouldn't work on the live system, got it sorted now, thats for all your help lads!
 
Status
Not open for further replies.
Top