I am trying to use these if/else if statements to display these php pages. The if/elseif statements allow for the php page to show up. The data is stored in the mysql. How do we
You need to use == instead of = I don't know what the right side of the statement is, for example Politics. If its not a variable then you should put it in quotes.
Something like this
if ($result_array[0] == "Politics") {
require 'news/political.php';
}else ...
Something like:
$pages = array(
'Politics' => 'political',
'Gossip' => 'celebgossib',
...
);
$used = array();
for ($i = 0; $i < 2; ++$i)
{
if (array_key_exists($result_array[$i], $pages)
{
if (!array_key_exists($result_array[$i], $used))
{
# only display this section once
include 'news/'.$pages[$result_array[$i]].'.php';
$used[$result_array[$i]] = true;
}
}
else
{
echo "Nothing to see here.";
}
}
I'm not sure exactly what you want to do if the page isn't found; or if subsequent records are duplicates.
How about using a switch statement?
switch $result_array[0] {
case 'Politics': include('politics.php'); break;
case 'This': include(...); break;
case 'That': include(....); break;
default: include('default.php'); break;
}
for a loooong set of if/then/else tests with simple "todo" sections, a switch statement is ideal.
It looks like you're iterating over an array of options, and including a bunch of set files?
I would try something like the following:
switch( TRUE )
{
case in_array("Politics", $result_array):
require 'news/political.php';
break;
case in_array("Gossip", $result_array):
require 'news/celebgossib';
break;
// etc.
}