ok, i know how to use the basic if statements, but this is what im trying to so i wanna know if its possible, i got this site, im trying to make it never leave index.php by making ALL content dynamic, so right now i got its like index.php?cat=3&page=1 i have the different menus for each category. can i use a if statement to show the different menus? the idea i got is i can do something like this. if cat=3 then echo "code for table here"; would it even be possible? cause this way looks to be sloppy. maybe righting functions, but ive never done that.
if statements
Page 1 of 19 Replies - 1644 Views - Last Post: 09 June 2005 - 12:14 PM
Replies To: if statements
#2
Re: if statements
Posted 08 June 2005 - 10:06 AM
I would suggest using includes rather than echoing, this will be more organized in the long run. And I would use a switch statement rather than an if for this situation:
<?
switch ($cat) {
case 0:
include("cat0_menu.php");
break;
case 1:
include("cat1_menu.php");
break;
case 2:
include("cat2_menu.php");
break;
}
?>
#3
Re: if statements
Posted 08 June 2005 - 11:51 AM
ok? so if cat = 1 then i would make that case 1? i dont know very much about the switch function.
#4
Re: if statements
Posted 08 June 2005 - 12:01 PM
i answered my own question. now i ran into a little problem. i have the page load with default values for cat and page. the switch dont load those...
the c_a="1"; and the p_a="3"; those are the defaults. if i type them into the address it works fine. how can i make so it loads right. take a look.
http://www.ask4tech.net/ask.php this loads the default. the content will load, but no navigation on left
http://www.ask4tech....hp?cat=1&page=3 this will load everything.
<?php
$c_a = "1";
if (isset($_GET['cat'])) {
$c_a = (get_magic_quotes_gpc()) ? $_GET['cat'] : addslashes($_GET['cat']);
}
$p_a = "3";
if (isset($_GET['page'])) {
$p_a = (get_magic_quotes_gpc()) ? $_GET['page'] : addslashes($_GET['page']);
}
mysql_select_db($database_conn, $conn);
$query_a = sprintf("SELECT * FROM site WHERE catID = %s AND pageID = %s", $c_a,$p_a);
$a = mysql_query($query_a, $conn) or die(mysql_error());
$row_a = mysql_fetch_assoc($a);
$totalRows_a = mysql_num_rows($a);
?>
the c_a="1"; and the p_a="3"; those are the defaults. if i type them into the address it works fine. how can i make so it loads right. take a look.
http://www.ask4tech.net/ask.php this loads the default. the content will load, but no navigation on left
http://www.ask4tech....hp?cat=1&page=3 this will load everything.
#5
Re: if statements
Posted 08 June 2005 - 12:24 PM
The only thing I can think of is that you are exiting before the nav menu code can execute.
#6
Re: if statements
Posted 08 June 2005 - 12:33 PM
???
here is the code i got so far.
here is the code i got so far.
<?php require_once('Connections/conn.php'); ?>
<?php
$c_a = "1";
if (isset($_GET['cat'])) {
$c_a = (get_magic_quotes_gpc()) ? $_GET['cat'] : addslashes($_GET['cat']);
}
$p_a = "3";
if (isset($_GET['page'])) {
$p_a = (get_magic_quotes_gpc()) ? $_GET['page'] : addslashes($_GET['page']);
}
mysql_select_db($database_conn, $conn);
$query_a = sprintf("SELECT * FROM site WHERE catID = %s AND pageID = %s", $c_a,$p_a);
$a = mysql_query($query_a, $conn) or die(mysql_error());
$row_a = mysql_fetch_assoc($a);
$totalRows_a = mysql_num_rows($a);
?>
<html>
<head>
<script type="text/javascript" src="milonic_src.js"></script>
<link href="styles.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
body,td,th {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
color: #000000;
}
-->
</style><div class=milonic><a href="http://www.milonic.com/">Javascript Menu, DHTML Menu Powered By Milonic</a></div>
<script type="text/javascript">
if(ns4)_d.write("<scr"+"ipt type=text/javascript src=mmenuns4.js><\/scr"+"ipt>");
else _d.write("<scr"+"ipt type=text/javascript src=mmenudom.js><\/scr"+"ipt>");
</script>
<script type="text/javascript" src="menu_data.js"></script>
<title>ASK Technology Solutions :: <?php echo $row_a['title']; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table id="Table_01" width="100%" height="425" border="0" cellpadding="0" cellspacing="0">
<tr>
<td colspan="15" background="images/bg.jpg">
<img src="images/ask_01.jpg" width="740" height="118" alt=""></td>
</tr>
<tr valign="top" background="images/ask_15.jpg">
<td colspan="14" background="/images/ask_15.jpg">
<p align="right"><img src="images/ask_14.jpg" width="44" height="28"></p> </td>
<td background="/images/ask_15.jpg"><img src="images/ask_15.jpg" width="13" height="28" align="right"></td>
</tr>
<tr>
<td colspan="7" valign="top"><?
switch ($cat) {
case 1:
include("cat1_menu.php");
break;
case 2:
include("cat2_menu.php");
break;
case 3:
include("cat3_menu.php");
break;
}
?></td>
<td colspan="8" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="2">
<tr>
<td><?php echo $row_a['content']; ?></td>
</tr>
</table></td>
</tr>
<tr>
<td>
<img src="images/spacer.gif" width="51" height="1" alt=""></td>
<td>
<img src="images/spacer.gif" width="13" height="1" alt=""></td>
<td>
<img src="images/spacer.gif" width="65" height="1" alt=""></td>
<td>
<img src="images/spacer.gif" width="15" height="1" alt=""></td>
<td>
<img src="images/spacer.gif" width="67" height="1" alt=""></td>
<td>
<img src="images/spacer.gif" width="14" height="1" alt=""></td>
<td>
<img src="images/spacer.gif" width="9" height="1" alt=""></td>
<td>
<img src="images/spacer.gif" width="42" height="1" alt=""></td>
<td>
<img src="images/spacer.gif" width="12" height="1" alt=""></td>
<td>
<img src="images/spacer.gif" width="67" height="1" alt=""></td>
<td>
<img src="images/spacer.gif" width="12" height="1" alt=""></td>
<td>
<img src="images/spacer.gif" width="169" height="1" alt=""></td>
<td>
<img src="images/spacer.gif" width="147" height="1" alt=""></td>
<td>
<img src="images/spacer.gif" width="44" height="1" alt=""></td>
<td>
<img src="images/spacer.gif" width="13" height="1" alt=""></td>
</tr>
</table>
</body>
</html>
<?php
mysql_free_result($a);
?>
#7
Re: if statements
Posted 08 June 2005 - 04:43 PM
I haven't gone over the code with a fine toothed comb. But I did notice that you never declare $cat (in switch($cat)) anywhere in that file.
#9
Re: if statements
Posted 09 June 2005 - 12:29 AM
iamcenz, on Jun 8 2005, 12:01 PM, said:
now i ran into a little problem. i have the page load with default values for cat and page. the switch dont load those...
You can end a swtich statement with the default: case and thereby catch whatever your final, default case is (i.e. anything not handled by the previous cases). Much cleaner than a series of IF statements. Though not as clean as just looking up the ID in your database, or numbering the files to include to correspond with the IDs.
#10
Re: if statements
Posted 09 June 2005 - 12:14 PM
i added a default to the case to load the cat1_menu, now it works perfectly. thanks
Page 1 of 1
|
|

New Topic/Question
This topic is locked




MultiQuote





|