The problem is fairly simple, yet hard to quite explain. I'm pulling the data 'url' and 'section' from a database. The 'section' value is an integer. I'm pulling all of this data into another array to be displayed onto another page. (This is a sitemap generator)
<?php
$query = "SELECT url, section FROM table WHERE section <= 3";
$result = db_query($query, $dbh);
$dynamic_urls = array();
while ($row = db_fetch_array($result))
{
$dynamic_urls[] = "http://example.com/" . checkSection() . $row['url'];
}
function checkSection(){
if ($row['section'] == 1)
{
return "web/";
}
elseif ($row['section'] == 2)
{
return "email/";
}
elseif ($row['section'] == 3){
return "print/";
}
return "FAILED/";
}
?>
I believe the problem is that in the checkSection() method is having trouble comparing the array value $row['section'] to an integer (1, 2, 3) as shown. The output file just loops through the array $dynamic_urls and prints them to a page, here is some example output:
http://example.com/FAILED/example_permalink1
http://example.com/FAILED/example_permalink2
http://example.com/FAILED/example_permalink3
http://example.com/FAILED/example_permalink4
I'm needing those 'FAILED' outputs to be the relevant 'web/', 'email/', or 'print/' titles that I'm looking for.
Why is my if statement having trouble comparing $row['section_id'] to an integer?
The problem is that your row isn't being received within the scope of your function.
function checkSection(){
if ($row['section'] == 1)
{
return "web/";
}
elseif ($row['section'] == 2)
{
return "email/";
}
elseif ($row['section'] == 3){
return "print/";
}
return "FAILED/";
}
To the function, $row is blank. You need to pass it as a variable to the function for this to work.
ie.
function checkSection($row){
if ($row['section'] == 1)
{
return "web/";
}
elseif ($row['section'] == 2)
{
return "email/";
}
elseif ($row['section'] == 3){
return "print/";
}
return "FAILED/";
}
checkSection($row)