Jump to content

[SOLVED] functions and if statement all together


redarrow

Recommended Posts

now as you can see from the code you click a link then the function is suppose to show but i keep getting errors strang stuff lol.

i wont to for pratice do it this way is it possable with only two pages cheers.

so in essance is it possable to get the same result for the members on  pages yes if we divided the pages to three or one it will work but what about two.

as you can see becouse the link is on the same page as the function it's hard  thanks anyway.




remeber funtion page and link

profile.php
[code]

<?php session_start();


$userid="member_0001";
echo "<a href='result.php?userid=$userid'>$userid</a><br>";

function member_one(){

$name="redarrow";
$location="london";
$message="have a good xmas and a happy new year";
echo "$name <br> $location <br> $message <br>";
}


$userid="member_0002";
echo "<a href='result.php?userid=$userid'>$userid</a><br>";

function member_two(){
$name="john";
$location="london";
$message="have a good xmas";
echo "$name <br> $location <br> $message <br>";
}


$userid="member_0003";
echo "<a href='result.php?userid=$userid'>$userid</a><br>";

function member_three(){
$name="petter";
$location="london";
$message="happy new year";
echo "$name <br> $location <br> $message <br>";
}


?>

[/code]


result.php

[code]

<?php

include ("profile.php");

if($_GET["userid"]=="member_0001"){

member_one()

}elseif($_GET["userid"]=="member_0002"){

member_two()

}elseif($_GET["userid"]=="member_0003"){

member_three()

}
?>

[/code]
You've forgotten to end three statements in result.php with semi-colons.

Another way to write result.php would be to use a switch statement:

[code]<?php
include ("profile.php");
switch ($_GET['userid']) {

case "member_0001":
member_one();
break;
case "member_0002":
member_two();
break;
case "member_0003":
member_three();
break;
}
?>[/code]

Ken
ken cheers

but now ken how can i get the links not to show on the resulting page even theo the links ae included within the other page.

for example there not there whent the results are present can that be done cheers.

[code]
<?php

include ("profile.php");

if($_GET["userid"]=="member_0001"){

member_one();

}elseif($_GET["userid"]=="member_0002"){

member_two();

}elseif($_GET["userid"]=="member_0003"){

member_three();

}
?>
[/code]
In profile.php, put this "if" statement
[code]<?php
if (!isset($_GET['userid']))
?>[/code]

before each link echo.

Actually, you can tighten up the code of profile.php considerably:
[code]<?php session_start();

for ($i=1;$i<4;$i++) {
if (!isset($_GET['userid'])) {
$userid='member_' . sprintf("%04d",$i);
echo "<a href='result.php?userid=$userid'>$userid</a><br>";
}
}

function member_one(){

$name="redarrow";
$location="london";
$message="have a good xmas and a happy new year";
echo "$name <br> $location <br> $message <br>";
}

function member_two(){
$name="john";
$location="london";
$message="have a good xmas";
echo "$name <br> $location <br> $message <br>";
}

function member_three(){
$name="petter";
$location="london";
$message="happy new year";
echo "$name <br> $location <br> $message <br>";
}
?>[/code]

If you really want tighten your code more, you can do this for profile.php:
[code]<?php session_start();

for ($i=1;$i<4;$i++) {
if (!isset($_GET['userid'])) {
$userid='member_' . sprintf("%04d",$i);
echo "<a href='result.php?userid=$userid'>$userid</a><br>";
}
}

function member($uid){
switch ($uid) {
case 'member_0001':
$name="redarrow";
$location="london";
$message="have a good xmas and a happy new year";
break;
case 'member_0002':
$name="john";
$location="london";
$message="have a good xmas";
break;
case 'member_0003':
$name="petter";
$location="london";
$message="happy new year";
break;
}
echo "$name <br> $location <br> $message <br>";
}
?>[/code]

and this for result.php:
[code]<?php
include ("profile.php");
member($_GET['userid']);
?>[/code]

Ken
keen want to brake this down ok this is really good code pratice ok hope you can help ok and cheers.

[code]

for ($i=1;$i<4;$i++) { <<four loop only showing four links// got that


if (!isset($_GET['userid'])) { << if the user id is set then show else dont


$userid='member_' . sprintf("%04d",$i); <<< what all this please exsplain cheers and thank you.

echo "<a href='result.php?userid=$userid'>$userid</a><br>";
}
}
[/code]


my teacher told me you dont need to no swithch statement there not needed what an xxxxx
The for loop is showing 3 links, not 4.

The line
[code]<?php
$userid='member_' . sprintf("%04d",$i);
?>[/code]

creates them userids "member_0001", "member_0002", "member_0003" using the function [url=http://www.php.net/sprintf]sprintf()[/url].

Ken

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.