Jump to content

Recommended Posts

Hello, I've been learning PHP for about 3 weeks now and I've came across a problem. I'm fairly new at this PHP stuff, and I'd appreciate some help.

 

What I got:

 

Index.php

<?php
@$uct = $_GET['uct'];	/* gets the variable $uct */
if (!empty($uct)) {
$uct .= '.php';
include($uct);
}	/* if $uct has a value, include it */
else {
include('home.php');
} 	/* otherwise, include the default uct */
?>

 

Home.php

<?php
$default = "This is the default code I want to show"
$1 = "This is going to be The content if ID = 1"
$2 = "This is going to be The content if ID = 2"
$3 = "This is going to be The content if ID = 3"
$4 = "This is going to be The content if ID = 4"
?>

 

Profiles.php

<?php
$default = "This is the default code I want to show"
$1 = "This is going to be The content if ID = 1"
$2 = "This is going to be The content if ID = 2"
$3 = "This is going to be The content if ID = 3"
$4 = "This is going to be The content if ID = 4"
?>

 

What I would like:

I'd like to be able to access the data I want by sorting it in my navigation. Something like index.php?uct=home&id=2 Or even index.php?uct=profiles&id=2

 

Thanks in advanced!:

I appreciate anyone's help or suggestions. If someone could show me how to write out the code so it works how I want it, that'd be great! Once again, thanks in advance. :confused:

Link to comment
https://forums.phpfreaks.com/topic/178637-_get-help-needed/
Share on other sites

You mean something like this:

<?php
if (is_int($_GET['id'])){
switch ($_GET['id']){
	case 1:
		$output = "This is going to be The content if ID = 1";
		break;
	case 2:
		$output = "This is going to be The content if ID = 2";
		break;
	case 3:
		$output = "This is going to be The content if ID = 3";
		break;
	case 4:
		$output = "This is going to be The content if ID = 4";
		break;
	default:
		$output = "This is the default code I want to show";
}
echo $output;
}
else{
exit("invalid ID number");
}

?

If this isn't what you want, let me know. If you want to learn about switches, take a look here

Link to comment
https://forums.phpfreaks.com/topic/178637-_get-help-needed/#findComment-942254
Share on other sites

@jonsjava:

 

watch using is_int() .. vairables passed via forms and urls are passed as strings, not integers, therefore, is_int() will return false (invalid id number) in the example you provided.

 

better to use is_numeric() (not much of a choice in this matter), and use is_int() if testing integers within your scripts.

Link to comment
https://forums.phpfreaks.com/topic/178637-_get-help-needed/#findComment-942258
Share on other sites

better to use is_numeric() (not much of a choice in this matter), and use is_int() if testing integers within your scripts.

 

Or ctype_digit, or filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT, array('options' => array('min_range' => 1, 'max_range' => 4))) for a slightly (ahem) more verbose option.

 

 

Link to comment
https://forums.phpfreaks.com/topic/178637-_get-help-needed/#findComment-942260
Share on other sites

You mean something like this:

<?php
if (is_int($_GET['id'])){
switch ($_GET['id']){
	case 1:
		$output = "This is going to be The content if ID = 1";
		break;
	case 2:
		$output = "This is going to be The content if ID = 2";
		break;
	case 3:
		$output = "This is going to be The content if ID = 3";
		break;
	case 4:
		$output = "This is going to be The content if ID = 4";
		break;
	default:
		$output = "This is the default code I want to show";
}
echo $output;
}
else{
exit("invalid ID number");
}

?

If this isn't what you want, let me know. If you want to learn about switches, take a look here

Like I have stated before, I'm not too good at PHP, but I can read what you have posted, and I am looking for something like that.

But I tried the code you gave me, with the link "index.php?uct=home&id=2" and I still got no luck :(

Any more suggestions?

Link to comment
https://forums.phpfreaks.com/topic/178637-_get-help-needed/#findComment-942292
Share on other sites

switch out this line:

 

if (is_int($_GET['id'])){

 

with:

 

if (ctype_digit ($_GET['id'])){

That worked, but now if you dont have an ID with the page, it'll show an error. I fixed this by adding a @. Check it out

<?php
if (@ctype_digit ($_GET['id'])){
   switch ($_GET['id']){
      case 1:
         $output = "This is going to be The content if ID = 1";
         break;
      case 2:
         $output = "This is going to be The content if ID = 2";
         break;
      case 3:
         $output = "This is going to be The content if ID = 3";
         break;
      case 4:
         $output = "This is going to be The content if ID = 4";
         break;
      default:
         $output = "This is the default code I want to show";
   }
   echo $output;
}
else{
   echo("invalid ID number");
}
?>

 

Deff worked. Will be using the forum more, so look out for me!

Thanks once again for everything!

Link to comment
https://forums.phpfreaks.com/topic/178637-_get-help-needed/#findComment-942305
Share on other sites

just lose the initial IF statement and use the 'default:' within the switch():

 

<?php
   switch (ctype_digit ($_GET['id'])){
      case 1:
         $output = "This is going to be The content if ID = 1";
         break;
      case 2:
         $output = "This is going to be The content if ID = 2";
         break;
      case 3:
         $output = "This is going to be The content if ID = 3";
         break;
      case 4:
         $output = "This is going to be The content if ID = 4";
         break;
      default:
         $output = "This is the default code I want to show";
   }
   echo $output;
?>

 

what happens is that the switch() statement matches the condition to the appropriate case within the switch, and if there are no matches, it uses the default.

Link to comment
https://forums.phpfreaks.com/topic/178637-_get-help-needed/#findComment-942308
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.