Jump to content

[SOLVED] problem with my "else" statment


dazzclub

Recommended Posts

Hi guys and girls,

 

I am trying to write a function along the lines of...

 

get variables from url, use to retrieve from a database and update pages title, if not use the default tiltle instead.

 

The ones i cant retrieve from the database will display their own title, these default titles will be set in the page with their own variable;

 

$page_title = 'Home page';

 

Here is what i have got so far

 

function pageTitle()
{
if ((isset($_GET['medical_product'])) && (isset($_GET['subcat_id'])) && (is_numeric($_GET['medical_product'])) && (is_numeric($_GET['subcat_id']))) { //correctly accessed 
$medical_product=$_GET['medical_product'];
$subcat_id=$_GET['subcat_id']; 
} else { 
$errors[] = 'You have accessed this page incorrectly.'; 
}
global $dbc;
$query="SELECT * FROM SubCat1 WHERE CategoryID = $medical_product AND SubCatID = $subcat_id";
$result = mysqli_query ($dbc, $query)or die(mysqli_error() . "<p>With query:<br>$query");
while ($row = mysqli_fetch_array($result))
{
echo "$row[subCatName]";
}
}

 

I'm struggling to find where to put my else statment to display the default title

 

Default title i.e

else {
$page_title;
}

 

Thanks for any help :)

Link to comment
https://forums.phpfreaks.com/topic/151318-solved-problem-with-my-else-statment/
Share on other sites

Hi guys,

 

Thanks for your advice.

 

What i went with at the end was switch statment.

 

If the function couldnt grab the right vairables from the url the swtich statment will look for a variable called $action which i placed in the urls.

 

index.php?action=news

 

So my final code now looks like this

 

function pageTitle()
{
if ((isset($_GET['medical_product'])) && (isset($_GET['subcat_id'])) && (is_numeric($_GET['medical_product'])) && (is_numeric($_GET['subcat_id']))) { //correctly accessed 
$medical_product=$_GET['medical_product'];
$subcat_id=$_GET['subcat_id']; 
global $dbc;
$query="SELECT * FROM SubCat1 WHERE CategoryID = $medical_product AND SubCatID = $subcat_id";
$result = mysqli_query ($dbc, $query)or die(mysqli_error() . "<p>With query:<br>$query");
while ($row = mysqli_fetch_array($result))
{
echo "$row[subCatName]";
}
} else {
switch ($_GET['action'])
{
case 'about';
echo ' - about us';
break;
case 'news';
echo ' - read all the latest news and our press release';
break;
case 'contact';
echo ' - make an enquiry';	
break;
case 'quality';
echo ' - quality';
break;
case 'home';
echo 'default title';
break;
}
}
}

 

If you still see areas of improvement then please let me know, im only a new-b :)

 

Thank you

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.