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
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

 

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.