segana Posted June 24, 2010 Share Posted June 24, 2010 Hi Guys, Complete Noob question coming (still getting to grips with PHP & MySQL), but it's doing my head in. I'm sure there's a better way of doing what I'm trying to do, but I'd just like to get things working using the method I'm already using, so any help would be greatly appreciated...anyway: The below code is part of an AJAX call. It reads a mysql table's and populates a HTML table row with a name (pulled from database) and a checkbox. It also reads another table and populates an array, which I then use an 'if statement' + in_array() to decide whether the checkbox in each row is checked or not. The issue I have is that if the mysql generated array contains the values '1,2,3,4' then the first 4 check boxes are checked, however if the mysql generated array contains the values '1,2,3,5' (or any number over 4) then only the first 3 checkboxes are checked. echo "<table"; while($prod = mysql_fetch_assoc($result)) { $itemArray = mysql_fetch_array($itemRun); echo "<tr> <td>".$prod['pName']."</td>"; if (in_array($prod['prodID'], $itemArray)) { ?> <td><input checked name="chk" type="checkbox" id="<?php echo $prod['prodID']; ?>" value="<?php echo $prod['pName']; ?>" onClick="chkit(<?php echo $prod['prodID']; ?>,this.checked);" /></td> <?php }else{ ?> <td><input name="chk" type="checkbox" id="<?php echo $prod['prodID']; ?>" value="<?php echo $prod['pName']; ?>" onClick="chkit(<?php echo $prod['prodID']; ?>,this.checked);" /></td> <?php }; echo "<tr>"; }; echo "</table"; However, to test my code, I changed: $itemArray = mysql_fetch_array($itemRun); to $itemArray = array(1,2,3,5); At which point checkboxes 1,2,3 & 5 were all checked correctly!! Why when using the mysql generated array does my code only seem to recognise the values 1,2,3 & 4 whereas if I manually set the array, the code works fine?? I'm probably missing something really simple or maybe I just don't really understand how in_array() actually works. Please, any help on this is greatly appreciated! Link to comment https://forums.phpfreaks.com/topic/205754-issue-with-in_array/ Share on other sites More sharing options...
Adam Posted June 24, 2010 Share Posted June 24, 2010 From the looks of things you're using mysql_fetch_array wrong. With each call it only returns 1 row at a time, you need to loop through it to check every row. I'm guessing it's just a coincidence that since the call is already within a loop, it works up until 3. If you print_r $itemArray within each iteration of the while loop it'll probably make more sense.. Link to comment https://forums.phpfreaks.com/topic/205754-issue-with-in_array/#findComment-1076709 Share on other sites More sharing options...
segana Posted June 28, 2010 Author Share Posted June 28, 2010 Hi MrAdam, Thanks for the reply. I figured I was using something wrong. Just to let you know, after going round and round trying to write a solution I had a bit of a brainwave which may be a long(ish) way of doing things, but for now...it works lol. $dynVari = '1'; while ($itemArray = mysql_fetch_array($itemRun)) { ${"a{$dynVari}"} = $itemArray[0]; $dynVari++; }; $checkedArray = array($a1,$a2,$a3,$a4); This way I could then use the below as all the variables had been loaded into the array $checkedArray. This worked a treat. if (in_array($prod['prodID'],$checkedArray)) As I said before, I have no doubt that there is a much quicker and easier way to do what I wanted to do, but only people who are a lot smarter and have much more experience with PHP probably know what that way is lol. Link to comment https://forums.phpfreaks.com/topic/205754-issue-with-in_array/#findComment-1078092 Share on other sites More sharing options...
Adam Posted June 28, 2010 Share Posted June 28, 2010 $dynVari = '1'; while ($itemArray = mysql_fetch_array($itemRun)) { ${"a{$dynVari}"} = $itemArray[0]; $dynVari++; }; $checkedArray = array($a1,$a2,$a3,$a4); Yeah this is the long way of doing it. You're also limited to a set number of results in the $checkedArray. Much simpler method: $checkedArray = array(); while ($itemArray = mysql_fetch_array($itemRun)) { $checkedArray[] = $itemArray[0]; } Link to comment https://forums.phpfreaks.com/topic/205754-issue-with-in_array/#findComment-1078108 Share on other sites More sharing options...
segana Posted June 28, 2010 Author Share Posted June 28, 2010 Cheers for that...much simpler! As I said, much smarter and more experienced people would know better ways Again, thanks for your help. It's been greatly appreciated! Link to comment https://forums.phpfreaks.com/topic/205754-issue-with-in_array/#findComment-1078112 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.