Page 1 of 1

If within if!?!

Posted: Sun Jan 14, 2007 9:27 am
by jmansa
I'm kind of newbie but I'm trying to make a golfsite where I can show pictures of each hole from a certain golfclub based on clubID. I can get the club by using clubinfo.php?clubid=24 in the browser adress, but how do I create links that refreshes the page with the same clubid but then also with a holeid??? To give you an idea what I mean here is some code I'm working with!

Code: Select all

<?php 
if (($_GET['clubid']) && (is_numeric($_GET['clubid']))) {
$clubid = $_GET['clubid'];

$dbhost = 'localhost';
$dbuser = 'username';
$dbpass = 'password';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                    ("Error connecting to mysql: " . mysql_error());

$dbname = 'mydb';
mysql_select_db($dbname);

$query="SELECT * FROM clubs WHERE ClubID='".$clubid."'"; 
$result=mysql_query($query) or die("Unable to find requested user");
$currUser = mysql_fetch_array($result);

?>
<?php

//18 holes

	if($currUser['course_18']=="yes") { 
		echo '<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td>';
		echo '<table width="60" border="0" cellspacing="0" cellpadding="0"><tr>';
		echo '<td><div align="center"><a href="link1">Hole 1</a></div></td>';
		echo '<td><div align="center"><a href="link2">Hole 2</a></div></td>';
		echo '<td><div align="center"><a href="link3">Hole 3</a></div></td>';
		echo '</tr></table>';
		echo '</td></tr>';
//Some if & elseif statements here to decide wich picture to show depinding on links!

	if...........
		echo '<tr><td>Hole 1 image here!</td></tr></table>';
	elseif.......
		echo '<tr><td>Hole 2 image here!</td></tr></table>';
	else.........
		echo '<tr><td>Hole 3 image here!</td></tr></table>';
		
//9 holes
		} else($currUser['course_9']=="yes") { 
		echo "Some code for viewing 9 holes!";
?>
So how do I get "hole=2" after the above line and how do I make that happen in my "link1, link2 and link3" links?

and can I put if and ifelse statements within if statements, and if so, how.

Hope that somebody can help.

Posted: Sun Jan 14, 2007 10:03 am
by Ollie Saunders
so how do I get "hole=2" after the above line and how do I make that happen in my "link1, link2 and link3" links?
I don't see those links in your code.
and can I put if and ifelse statements within if statements, and if so, how.
Yes you can, it is called "nesting" and is frequently used in programming. Reading the manual on control structures might help but surprisingly there isn't an example of nesting so I'm going to do one for you:

Code: Select all

if (($_GET['clubid']) && (is_numeric($_GET['clubid']))) {
    if ($_GET['clubid'] == 4) {
        echo 'clubid is 4';
    } else {
        echo 'cludid is something else';
    }
} else {
    echo 'No or bad clubid';
}

Posted: Sun Jan 14, 2007 10:15 am
by jmansa
Thanks alot. That helped on some of the problem.

The links is here!
if($currUser['course_18']=="yes") {
echo '<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td>';
echo '<table width="60" border="0" cellspacing="0" cellpadding="0"><tr>';
echo '<td><div align="center"><a href="link1">Hole 1</a></div></td>';
echo '<td><div align="center"><a href="link2">Hole 2</a></div></td>';
echo '<td><div align="center"><a href="link3">Hole 3</a></div></td>';
echo '</tr></table>';
echo '</td></tr>';

Hope you have an idea for that!

Posted: Sun Jan 14, 2007 10:25 am
by Ollie Saunders
Oh sorry I'm blind.
I think what you want to do is add GET params to those links a bit like this:

Code: Select all

$link = 'link1?clubid=' . urlencode($_POST['clubid']) . '&anotherParam=anotherValue';
echo '<a href="'. $link .'">Hole 1</a>';
Or if you've got PHP 5 http_build_query() is good.

Posted: Sun Jan 14, 2007 11:50 am
by Cody Mays
I suggest you look into SQL injection prevention on your query string (variable passed via the url). I suggest you set $clubid to mysqli_real_escape_string($_GET['clubid']);