Jump to content

[SOLVED] multiple conditions in a foreach loop


unknown1

Recommended Posts

Well, conditions wouldn't be the right word to use, because they're not really conditions. They're more like expressions.

As far as I know, there are no ways to (linearly) loop through two arrays using only the foreach loop.

 

That said,

<?php
while(list(,$rdate) = each($d) && list(,$url) = each($u)) {

}
?>

 

Would be comparable to your desired loop, although it's a bit difficult to read. There are cleaner ways of doing what you're suggesting.

Link to comment
Share on other sites

Well, conditions wouldn't be the right word to use, because they're not really conditions. They're more like expressions.

As far as I know, there are no ways to (linearly) loop through two arrays using only the foreach loop.

 

That said,

<?php
while(list(,$rdate) = each($d) && list(,$url) = each($u)) {

}
?>

 

Would be comparable to your desired loop, although it's a bit difficult to read. There are cleaner ways of doing what you're suggesting.

 

I assume the code above and will do the same thing as a foreach loop? 

Link to comment
Share on other sites

Yes, it will loop through both arrays linearly, terminating execution when one of the arrays runs out of data members.

Note this means that if one array (let's call it L) has more elements than the other array (let's call it S), the loop will only iterate through count(S) elements of L.

 

Okay, this is my script... and when I run it I get an error

 

Warning: Variable passed to each() is not an array or object in C:\xampp\htdocs\2\test.php on line 28 and script fails.

 

And yes both $u and $r have the same amount of elements

 

 

<?php

include('config.php');


$sql_rdate="SELECT * FROM table_tmp WHERE status='1'";
$rs_dt=mysql_query($sql_rdate) or die (mysql_error());

$sql_url="SELECT * FROM table_tmp WHERE status='1'";
$rs_url=mysql_query($sql_url) or die (mysql_error());

//while($r= mysql_fetch_assoc($rs_url)){

//$url[]=$r;


//}



while($row= mysql_fetch_assoc($rs_dt)&& $r= mysql_fetch_assoc($rs_url)){

   $rdate[]=$row;
   $url[]=$r;

}

while(list(,$rdate) = each($d) && list(,$url) = each($u)) {



$u['Url'] = trim($u['Url']);



//Remove whitespace in case there is some.
$d['rDate'] = trim($d['rDate']);

$expire=date('Y-m-d',strtotime($d['rDate'].' + 90 days'));



echo "$expire $u[url]<br>";

}




?>

 

Link to comment
Share on other sites

I'm sorry, I just skimmed your original code, not spending much attention. You might want to look into list() and each() to see what my mistake was, but the short fix is to swap $rdate with $d and $url with $u.

 

That said, why have two seperate loops? Just place the logic in this loop we're creating in the while loop that does mysql_fetch_assoc to begin with.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.