SomeGuy242323 Posted October 23, 2007 Share Posted October 23, 2007 Before I go into detail with the big question, my itching one is this: how do you call the last instance of a foreach loop? Now, the foreach is meant to be a loop for the physical size of the array, not the actual size. So I have a script that posts the first 5 topics of a forum, but if there aren't enough, it posts as many as it can, with the foreach loop. I want to add a copyright statement to the last topic, is there anyway to do this? Like: foreach($topics as $topic) ... if(isLast($topic)) echo($blah) Is that at all possible? Now for the real question...what is the scope of $_GET? To understand what I mean, let me post code. Index.php: <?php $page = $_GET["page"]; if(!file_exists('content/'.$page.'.php') || $page == null) include("content/news.php"); else include("content/".$page.".php"); ?> This works fine and dandy, no complaints. However, my news.php also needs to be about to $_GET a variable, to know which news item to post (if specified). So I have the code written as so in my news.php file: <?php $id = $GET_["id"]; $topics = $SDK->list_forum_topics ("2", array("limit" => "1", "start" => "0", "order" => "desc", "orderby" => "post_date"), "1"); if(!$id || $id == null) $topic = $topics[0]; else $topic = $SDK->get_topic_info($id); ?> This is supposed to get the $id variable, and assign $topic to it, but if there is none, or it's null, it takes the newest topic from the forum to post (which would be the newest news item). However, I've debugged this so many times, and it never prints out a $id. The links I use are /?id='$id' and I've also tried using /page=news&id='$id'. It just seems like it won't retrieve the $id file from the browser. How can I fix this? Edit: It's nothing to do with the $SDK class either. I tried a flat number for the last line '$topic = $SDK->get_topic_info(4);' and it worked perfectly. The problem deals with retrieving $id from the browser. Quote Link to comment https://forums.phpfreaks.com/topic/74484-solved-couple-of-php-questions_get-involved-also-foreach/ Share on other sites More sharing options...
Orio Posted October 23, 2007 Share Posted October 23, 2007 The variable is called $_GET, not $GET_ That's your problem. Also, it's recommended to put single quotes around array keys, and not double quotes so your line should be: $id = $_GET['id']; As for the array question, you could make a little trick like this: <?php foreach ($array as $val) { //Do all the things you want to do... if(next($array) === FALSE) { //This is the last one, do whatever you want } else { prev($array); //To undo what we did with next() //This is not the last one- continue here } } ?> I haven't checked the code, but I think it will get the job done. Orio. Quote Link to comment https://forums.phpfreaks.com/topic/74484-solved-couple-of-php-questions_get-involved-also-foreach/#findComment-376414 Share on other sites More sharing options...
SomeGuy242323 Posted October 23, 2007 Author Share Posted October 23, 2007 WOW! I can't believe I'm so stupid! That's the second time I did that with $_GET today...first time I used () instead of []. It's hard having Java as your native language and working in PHP. Also while you're very sick, lol (which is why I'm actually able to work on it, lol). Quote Link to comment https://forums.phpfreaks.com/topic/74484-solved-couple-of-php-questions_get-involved-also-foreach/#findComment-376417 Share on other sites More sharing options...
Orio Posted October 23, 2007 Share Posted October 23, 2007 My solution for the array problem didn't work... I'll try to think of something else. Orio. Quote Link to comment https://forums.phpfreaks.com/topic/74484-solved-couple-of-php-questions_get-involved-also-foreach/#findComment-376418 Share on other sites More sharing options...
Orio Posted October 23, 2007 Share Posted October 23, 2007 Well, I guess you'll have to go with the simple solution: <?php $i = 1; $size = count($array); foreach ($array as $val) { //Do all the things you want to do... if($i == $size) { //This is the last one, do whatever you want } else { //This is not the last one- continue here } $i++; //Dont forget this! } ?> This was tested, and it works Orio. Quote Link to comment https://forums.phpfreaks.com/topic/74484-solved-couple-of-php-questions_get-involved-also-foreach/#findComment-376421 Share on other sites More sharing options...
SomeGuy242323 Posted October 23, 2007 Author Share Posted October 23, 2007 I think I got it. Before the loop, I'll create a temp variable. $end = end($topics); ... if($topic === $end) //Last Array Code I could have figured that out, but I didn't know PHP even had array manipulators like that. Thanks for showing me the next() function, or else I would have never found my way around the PHP Manual to find what I needed, lol. Quote Link to comment https://forums.phpfreaks.com/topic/74484-solved-couple-of-php-questions_get-involved-also-foreach/#findComment-376423 Share on other sites More sharing options...
Orio Posted October 23, 2007 Share Posted October 23, 2007 Yeah, but that won't work if you have two identical values in the array. But if you think the values will always be different, it's ok. Orio. Quote Link to comment https://forums.phpfreaks.com/topic/74484-solved-couple-of-php-questions_get-involved-also-foreach/#findComment-376488 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.