crazyfrog Posted February 17, 2007 Share Posted February 17, 2007 Hi, Can anyone see a problem with this bit of code? <?php session_start(); foreach ($_SESSION['_amember_subscriptions'] as $sub); //This is line 53 if ($sub['payment_id']){ $diff = (strtotime($s['expire_date']) - time()) / (3600*24); if ($diff > 0){ print "Your subscription expires in $diff days"; print ("$sub"); } } ?> I am getting the following error and cant understand why. reading through other topics, i seem to think it is something to do with arrays? Warning: Invalid argument supplied for foreach() in /home/*****/public_html/tester.php on line 53 Thanks Link to comment https://forums.phpfreaks.com/topic/38901-foreach-help/ Share on other sites More sharing options...
redarrow Posted February 17, 2007 Share Posted February 17, 2007 <?php session_start(); $x=$_SESSION['_amember_subscriptions']; foreach($x as $sub){ if ($sub['payment_id']){ $diff = (strtotime($s['expire_date']) - time()) / (3600*24); if ($diff > 0){ print "Your subscription expires in $diff days"; print ("$sub"); } } } ?> Link to comment https://forums.phpfreaks.com/topic/38901-foreach-help/#findComment-187076 Share on other sites More sharing options...
crazyfrog Posted February 17, 2007 Author Share Posted February 17, 2007 Still getting the same error That was a damn fast reply though! Link to comment https://forums.phpfreaks.com/topic/38901-foreach-help/#findComment-187081 Share on other sites More sharing options...
hvle Posted February 17, 2007 Share Posted February 17, 2007 foreach ($_SESSION['_amember_subscriptions'] as $sub); //This is line 53 Warning: Invalid argument supplied for foreach() in /home/*****/public_html/tester.php on line 53 this is caused when $_SESSION['_amember_subscriptions'] is not an array. You may want to double check this. Also, giving a line like foreach ($_SESSION['_amember_subscriptions'] as $sub); // this is meaningless because you terminated the foreach loop with the semicolon ';' before it can actually do anything: foreach ($_SESSION['_amember_subscriptions'] as $sub) { // your code here } Link to comment https://forums.phpfreaks.com/topic/38901-foreach-help/#findComment-187082 Share on other sites More sharing options...
crazyfrog Posted February 17, 2007 Author Share Posted February 17, 2007 this is caused when $_SESSION['_amember_subscriptions'] is not an array. You may want to double check this. How would i find out? Link to comment https://forums.phpfreaks.com/topic/38901-foreach-help/#findComment-187085 Share on other sites More sharing options...
hvle Posted February 17, 2007 Share Posted February 17, 2007 var_dump(is_array($_SESSION['_amember_subscriptions'])); if you have bool (true), it is an array, false mean it is not. Link to comment https://forums.phpfreaks.com/topic/38901-foreach-help/#findComment-187088 Share on other sites More sharing options...
crazyfrog Posted February 17, 2007 Author Share Posted February 17, 2007 Damn, its not! Any remedies? Link to comment https://forums.phpfreaks.com/topic/38901-foreach-help/#findComment-187091 Share on other sites More sharing options...
hvle Posted February 17, 2007 Share Posted February 17, 2007 if it is not an array, there is no point to use foreach loop isn't it? Link to comment https://forums.phpfreaks.com/topic/38901-foreach-help/#findComment-187097 Share on other sites More sharing options...
crazyfrog Posted February 17, 2007 Author Share Posted February 17, 2007 Thanks for all your help, but i am new to php and am finding it quite interesting. would i just remove foreach from the code then? Link to comment https://forums.phpfreaks.com/topic/38901-foreach-help/#findComment-187099 Share on other sites More sharing options...
hvle Posted February 17, 2007 Share Posted February 17, 2007 Your code expect $sub to be an array as well. So, I am sure if you remove the foreach, it still won't work. Link to comment https://forums.phpfreaks.com/topic/38901-foreach-help/#findComment-187101 Share on other sites More sharing options...
crazyfrog Posted February 17, 2007 Author Share Posted February 17, 2007 Ah well. Really needed this to work. Thankyou very much for all your help. Link to comment https://forums.phpfreaks.com/topic/38901-foreach-help/#findComment-187111 Share on other sites More sharing options...
kenrbnsn Posted February 17, 2007 Share Posted February 17, 2007 How are you setting the session value? Ken Link to comment https://forums.phpfreaks.com/topic/38901-foreach-help/#findComment-187112 Share on other sites More sharing options...
crazyfrog Posted February 17, 2007 Author Share Posted February 17, 2007 How are you setting the session value? Ken I dont quite understand? I am trying to add a line of text to my site something like "Your subscription will end in ** days" Link to comment https://forums.phpfreaks.com/topic/38901-foreach-help/#findComment-187114 Share on other sites More sharing options...
kenrbnsn Posted February 17, 2007 Share Posted February 17, 2007 You want to use "$_SESSION['_amember_subscriptions']". How/where are you putting something into this session variable? Ken Link to comment https://forums.phpfreaks.com/topic/38901-foreach-help/#findComment-187116 Share on other sites More sharing options...
crazyfrog Posted February 17, 2007 Author Share Posted February 17, 2007 It is created in mysql when the user subscribes for a selected period. Hope this is what you mean Link to comment https://forums.phpfreaks.com/topic/38901-foreach-help/#findComment-187125 Share on other sites More sharing options...
kenrbnsn Posted February 17, 2007 Share Posted February 17, 2007 Please post the code that sets the value. Ken Link to comment https://forums.phpfreaks.com/topic/38901-foreach-help/#findComment-187128 Share on other sites More sharing options...
crazyfrog Posted February 17, 2007 Author Share Posted February 17, 2007 I think i will leave it, i can see this getting too technical for a N0ob. :'( I wouldnt know where to look. Thankyou Link to comment https://forums.phpfreaks.com/topic/38901-foreach-help/#findComment-187133 Share on other sites More sharing options...
crazyfrog Posted February 17, 2007 Author Share Posted February 17, 2007 Please post the code that sets the value. Ken Would this be something like the signup.php page source? Link to comment https://forums.phpfreaks.com/topic/38901-foreach-help/#findComment-187150 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.