Forum Moderators: coopster

Message Too Old, No Replies

Switch statement menu - Where's the bug?

         

Mtlinfo

3:12 am on Sep 7, 2006 (gmt 0)

10+ Year Member



I'm trying to duplicate a menu.

Basically it uses just one php page to display the content of the 18 different tables (areaguides, business, etc...).

If you click the Cities link, the $cat changes and display the word Cities and changes again to Business if you click the Business link.

The bug is that when you click the links, I don't want to see the word Cities but the Cities table content.

So, any idea where the bug might be in my code below?

Thanks,

Richard

= = = = = = = = = = = =
<?
mysql_connect(localhost,user,pw);
@mysql_select_db(my_db) or die( 'Unable to select database');
$query='SELECT * FROM Links';
$cat = $_GET['cat'];
switch($cat) {
case 'Cities':
$table = 'Cities';
break;
case '$Businesss':
$table = '$Business';
break;
default:
$table = 'Default_Table';
break;
}

echo "

<html>

<body>

<P>
<a href=1.php?cat=Cities>Cities</a>
<br>
<a href=1.php?cat=Business>Business</a>

<p>
$cat
</body>
</html>";

$i++;

?>

[edited by: dreamcatcher at 6:50 am (utc) on Sep. 7, 2006]
[edit reason] No URLs, thanks. [/edit]

smells so good

5:08 am on Sep 7, 2006 (gmt 0)

10+ Year Member



You've got a couple of 'buggy things' in your code, but nothing yet to display the table that you want. Weren't you trying to do this in another thread the other day?

First buggy thing is this switch statement.

case '$Businesss':
$table = '$Business';

Business must be spelled properly throughout, or the case will never match. Secondly, you have the case and the table both defined as variables. Let's fix this first.

case 'Business':
$table = 'Business';

The next buggy thing is this variable $i. It just bugs me, since it's not doing anything except incrementing. Throw it away unless you have another use for it.

$i++;

Now you are ready to display the table. I'm going to assume that your table data is in a database. For my example I'm going to extract a single field from a single row. You may need to extract several rows of data, and you can then arrange them as you like.

The first thing I'm checking is the variable $table. If it is set, then the script continues to a new switch statement. For each case I am extracting my data from the database, and then I'm displaying that data. Again, for simplicity sake, I am extracting and displaying a minimal amount of data.


if ($table) {
switch($table) {
case 'Cities':
$result = mysql_query("SELECT Contact FROM customer_master ORDER BY recid DESC LIMIT 0,1");
while ($line = mysql_fetch_array($result, MYSQL_BOTH)) {
$CUSTID = $line['Contact'];
echo "<br>$CUSTID";
}
break;
case 'Business':
$result = mysql_query("SELECT Customer_ID FROM customer_master ORDER BY recid DESC LIMIT 0,1 ");
while ($line = mysql_fetch_array($result, MYSQL_BOTH)) {
$CONTACT = $line['Customer_ID'];
echo "<br>$CONTACT";
}
break;
}
}

Now it's up to you to determine what data to use to build your tables, and how to format them on the page. Here's your script, as I modified it.


<?
mysql_connect(localhost,user,pw);
@mysql_select_db(my_db) or die( 'Unable to select database');
$query='SELECT * FROM Links';
$cat = $_GET['cat'];
switch($cat) {
case 'Cities':
$table = 'Cities';
break;
case 'Business':
$table = 'Business';
break;
default:
$table = 'Default_Table';
break;
}
echo "
<html>
<body>
<p>
<a href=1.php?cat=Cities>Cities</a>
<br>
<a href=1.php?cat=Business>Business</a>";
if ($table) {
switch($table) {
case 'Cities':
$result = mysql_query("SELECT Contact FROM customer_master ORDER BY recid DESC LIMIT 0,1");
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
$CUSTID = $row['Contact'];
echo "<br>$CUSTID";
// BUILD YOUR TABLE OF LINKS HERE
}
break;
case 'Business':
$result = mysql_query("SELECT Customer_ID FROM customer_master ORDER BY recid DESC LIMIT 0,1 ");
while ($row= mysql_fetch_array($result, MYSQL_BOTH)) {
$CONTACT = $row['Customer_ID'];
echo "<br>$CONTACT";
// BUILD YOUR TABLE OF LINKS HERE
}
break;
}
}
echo "</body>
</html>";

?>

The query located near the top of your script will likely be the one that you use, so replace my query to customer_master with your own.

Mtlinfo

6:06 am on Sep 7, 2006 (gmt 0)

10+ Year Member



Thanks Smell So Good for the nice long reply. :-)

I'm just a newbie in PHP so let's see if you can tell me where I went wrong because I uploaded your script and it gives me this error msg...

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in...1.php on line 28

Line 28 is this line...

while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {

My main table is called Links. When I click the Structure link I see a list of categories under the main column "Field" and this is where we find Cities, Business and others.

So, here's a few questions...

(1)

On the line...

$table = 'Default_Table';

...Should I leave it like this or replace Default_Table by Links?

(2)

Let's look at 1 specific case here (Cities). I replaced your Customer_ID by Cities and Customer_master with Links like this...

case 'Cities':
$result = mysql_query("SELECT Cities FROM Links ORDER BY recid DESC LIMIT 0,1");
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
$Cities = $row['Cities'];

...since it gives me an error msg, can you tell me where did I go wrong?

Thanks

Richard

ramoneguru

8:06 am on Sep 7, 2006 (gmt 0)

10+ Year Member



Add an "OR die(mysql_error());" to make sure you are actually getting results back from the query and spelling everything correctly.
--Nick

smells so good

8:23 am on Sep 7, 2006 (gmt 0)

10+ Year Member



I'll try to help.

#1 - It depends. What do you want the default action to do?

#2 - Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in...1.php on line 28
This is telling you that you are not getting any results from your select statement. It's entirely possible that you do not have a field named recid in your table. But your (my) SELECT statement is looking for that field. Rather than add a field, fix the to code to match what you do have in your table.

$result = mysql_query("SELECT Cities FROM Links WHERE Field = '$cat' ");

NOTE: 'Field' will be the name of the column that lists your categories. It sounds like you may have named that field as Field?

I'm going to suggest that you change the following line as well. It basically says to return the query results as an associative array.

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))

smells so good

6:33 pm on Sep 7, 2006 (gmt 0)

10+ Year Member



Let me take this opportunity to direct you to the Forum Charter [webmasterworld.com]. Besides defining what this forum is about and how to get the most from it, it has some excellent links for troubleshooting. Since you are new to PHP, those will be invaluable if you begin to learn some of these techniques early on.