Jump to content

[SOLVED] Couple of PHP questions...$_GET involved, also foreach.


SomeGuy242323

Recommended Posts

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.