0

I have this foreach statement that is supposed to cycle through an array, but instead of outputting:

[checkbox] Something Here

It will instead output this:

[checkbox] [checkbox] Something here

Basically, the if statement isn't being skipped over, but the foreach loop is looking at the values in my array (in this case I only have 2 for testing purposes) and processing them at the same time instead of separately. I've done some echo tests and that is what is happening, but why?

$blogID will have array values 56,57 so because the loop is processing both values at the same time, then both options in the if statement become true because 56 equals 56, but then 56 doesn't equal 57 on its 2nd pass so it will process the 2nd half of the if statement as true. This is weird and I've never had this happen before.

Here is the code:

$getblog = mysql_query("SELECT * FROM content WHERE blogID = '{$_REQUEST['id']}' AND type = '5' ORDER BY `order` ASC");
while ($row = mysql_fetch_assoc($getblog)){
$blogID = $row['id'];    
$tempData[$blogID][] = $row;

$data = $_REQUEST['blogIDS'];
$ids = explode(",", $data);

foreach($ids as $blogIDS) {
    $getblogids = mysql_query("SELECT * FROM content WHERE id = '$blogIDS' AND type = '5' ORDER BY `order` ASC");
    while ($row2 = mysql_fetch_assoc($getblogids)){
        $blogIDS = $row2['id'];    
        $tempData2[$blogIDS][] = $row2;
    }
}
    if ($row['id'] == $blogIDS) {
            echo "<input type='checkbox' name='ids[]' value='{$row['id']}' checked='yes'/>\n";
    } else {
            echo "<input type='checkbox' name='ids[]' value='{$row['id']}'/>\n";
    }

    echo "<a class='heriyah_text1' href='manage_blogposts_add.php?resize=1&edit=1&id={$row['id']}&blogID={$_REQUEST['id']}&pageID={$_REQUEST['pageID']}&div={$_REQUEST['div']}'>{$row['title']}</a><p></p>\n";
}
5
  • Where's the Something here part? Also, your elseif is totally redundant. Simply use else { echo ... Commented Mar 6, 2012 at 2:45
  • 2
    The code above will always output two checkboxes in sequence. Please post more of the surrounding code, because it isn't clear what is wrong here. Commented Mar 6, 2012 at 2:46
  • posted, I wasn't sure if that part was needed, but its up now! Thanks for asking! =) Commented Mar 6, 2012 at 2:48
  • I can't duplicate your results using the code you've provided. Please include your variables, in addition to the more code requested by Michael. Commented Mar 6, 2012 at 2:50
  • yes, that whole code is pretty confusing. It took me a while to sit through it and write it out. The while loop by itself works perfectly to get the individual data, but the foreach won't get me ONLY the checkbox I need. It will give me two checkboxes, one I need and the other that I dont. Commented Mar 6, 2012 at 2:54

2 Answers 2

2
+50

edited. try this and let me know:

$getblog = mysql_query("SELECT * FROM content WHERE blogID = '{$_REQUEST['id']}' AND 
                         type = '5' ORDER BY `order` ASC");
while ($row = mysql_fetch_assoc($getblog)){
    $blogID = $row['blogID'];    
    $tempData[$blogID][] = $row;
}

$data = $_REQUEST['blogIDS'];
$ids = explode(",", $data);

foreach($ids as $blogID) {
    if (array_key_exists($blogID, $tempData)) {
        foreach($tempData[$blogID] as $key => $value) {
            if ($value['blogID'] == $blogID) {
                echo "<input type='checkbox' name='ids[]' value='{$value['id']}' checked='yes'/>\n";
            } else {
                echo "<input type='checkbox' name='ids[]' value='{$value['id']}'/>\n";
            }

            echo "<a class='heriyah_text1' href='manage_blogposts_add.php?resize=1&edit=1&id={$value['id']}&blogID={$_REQUEST['id']}&pageID={$_REQUEST['pageID']}&div={$_REQUEST['div']}'>{$value['title']}</a><p></p>\n";
        }
    }
}
Sign up to request clarification or add additional context in comments.

13 Comments

this doesn't make any data show up now. I wish I could just show you the page, but it's inside an admin panel of my CMS. I'll keep messing around with it. Is the $blogID value being changed alot? you have $ids as $blogID which means one thing, but then we have $tempData[$blogID]` means something else.
okay I've gotten everything to show up now but it still doubles everything. I just changed some values, check and see if what I did messed it up, I'm just gonna post it in my original question if that's okay with you.
i used $blogID as a key in $tempData. when you iterate through your ids you are accesses that particular key in $tempData. you need to be checking $blogID in $tempData otherwise this otherwise it's really no different than your first answer. you've edited your original post to iterate using $bID now instead which is producing the same results as before.
also, it may be helpful to see that your query is returning expected results. i would add a var_dump($getblog); immediately after your $getblog assignment. copy this query and execute it in mysql and make sure you're getting what you expect.
I'll definitely try that! Let me see what I get!
|
1

You can do this one instead:

$getblog = mysql_query("SELECT * FROM content WHERE blogID = '{$_REQUEST['id']}' AND 
                     type = '5' ORDER BY `order` ASC");
while ($row = mysql_fetch_assoc($getblog)){
    $tempData[] = $row;
}

$data = $_REQUEST['blogIDS'];
$ids = explode(",", $data);

foreach($ids as $blogID) {
   foreach($tempData as $key => $value){
      if($value['id'] == $blogID){
         echo "<input type='checkbox' name='ids[]' value='{$value['id']}' checked='yes'/>\n";
      }else{
         echo "<input type='checkbox' name='ids[]' value='{$value['id']}'/>\n";
      }

      echo "<a class='heriyah_text1' href='manage_blogposts_add.php?resize=1&edit=1&id={$value['id']}&blogID={$_REQUEST['id']}&pageID={$_REQUEST['pageID']}&div={$_REQUEST['div']}'>{$value['title']}</a><p></p>\n";
   }
}

6 Comments

Hmm, that gets rid of the checkboxes doubling. Was it because the foreach was inside the while loop?
It's causing this to happen though, I can probably figure it out, but maybe you can help while I try to figure it out. CLICK HERE TO VIEW SCREENSHOT
@drummer392, the doubling is caused due to the fact it's within the while loop. let me think of the current problem you've encountered with the updated codes. basically it should only show blog post 1 to 4 right?
Yes sir, I appologize for not knowing how to fix the problem further. I have been learning PHP for the past 3 years now and still don't know how to do something like this. I really appreciate your help!
How many records does your query outputs? the 2nd foreach will loop through all the records from your query. The first foreach will loop through all the ids based on the $_REQUEST['blogIDS'] variable
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.