...

View Full Version : code inspection



grudz
01-22-2004, 05:28 PM
Hello,

I've been trying to learning PHP on my own, but im kinda lost with this code (it's just a test to help me understand the Switch statement)


<?php mysql_select_db($database_baseline, $baseline);
$query_Recordset1 = "SELECT name FROM listing";
$Recordset1 = mysql_query($query_Recordset1, $baseline) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
$pets = 2
?>
<?php switch(2)
{
case "2";
echo "$Recordset1";
break;
case "3";
echo "4";
break;
} ?>

why does it give me "Resource id #4 " instead of the "name" from my recordset?

thanx

Spookster
01-22-2004, 05:41 PM
If you print out $Recordset1 that is exactly what you have a reference to the recordset. That recordset gets returned as an associative array. To print out the data in the recordset you must specify it as an array with a reference to which column of the table you want to print out. Like so:






echo $Recordset1['columnname'];



columnname would be whatever column is available from what you selected in your query. In your case it would be




echo $Recordset1['name'];

grudz
01-22-2004, 05:50 PM
ok.....but i would understand if it returns "Recordset1" but it doesnt i shows me "Ressource id#4" whats that?

grudz
01-22-2004, 05:53 PM
alright so this would have to be the code?


<?php mysql_select_db($database_baseline, $baseline);
$query_Recordset1 = "SELECT name FROM listing";
$Recordset1 = mysql_query($query_Recordset1, $baseline) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
$pets = 2
?>
<?php switch(2)
{
case "2";
echo $Recordset1['name'];
break;
case "3";
echo "4";
break;
}

?>

cuz that doesnt work

Spookster
01-22-2004, 06:07 PM
Looking at your switch statement it looks like you have a data type mismatch and a few typos going on.



switch(2) {
case "2";
echo $Recordset1['name'];
break;
case "3";
echo "4";
break;
}


You have the integer 2 being passed into the switch statement but in your case you are treating it as a string. Those are two different datatypes.

2 does not equal "2". "2" is a string. 2 is an integer. If you surround a value in quotes it will be treated as a string.

You also should have colons at the end of the case lines instead of semi-colons.

This should be the correct code


switch(2) {
case 2:
echo $Recordset1['name'];
break;
case 3:
echo "4";
break;
}

grudz
01-22-2004, 06:47 PM
switch(2) {
case 2:
echo $row_Recordset1['name'];
break;
case 3:
echo "4";
break;
}


the one u gave me didnt work......it didnt show me nothing.....so i put $row_ and it worked....does it make sense?

grudz
01-22-2004, 06:58 PM
anyway.....here is my situation (and the reason why i wanted to learn about switch statements)

im still in the testing stage, after, when i get it and make it work....ill use it on my real website....

i have 3 pages: t1, t2, t3

t1 is where the statement is.....


<?php

switch($t2) {
case $t2:
echo "t2";
break;
case $t3:
echo "t3";
break;
}

?>

now my question is this....in my other pages (t2, t3)

how do i pass variables......like "if somebody clicks this....its this variable and then it'll go to my t1 page" ?

do u understand? cuz it's kinda hard to explain it

Spookster
01-22-2004, 09:05 PM
Originally posted by grudz

switch(2) {
case 2:
echo $row_Recordset1['name'];
break;
case 3:
echo "4";
break;
}


the one u gave me didnt work......it didnt show me nothing.....so i put $row_ and it worked....does it make sense?

That's my mistake. I was using the wrong variable. This brings up another good point about programming. Avoid using similar variable names for different things. It is easy to confuse which one was which. :thumbsup:

Spookster
01-22-2004, 09:18 PM
Originally posted by grudz
anyway.....here is my situation (and the reason why i wanted to learn about switch statements)

im still in the testing stage, after, when i get it and make it work....ill use it on my real website....

i have 3 pages: t1, t2, t3

t1 is where the statement is.....


<?php

switch($t2) {
case $t2:
echo "t2";
break;
case $t3:
echo "t3";
break;
}

?>

now my question is this....in my other pages (t2, t3)

how do i pass variables......like "if somebody clicks this....its this variable and then it'll go to my t1 page" ?

do u understand? cuz it's kinda hard to explain it

What exactly are you wanting to do? Are you wanting to redirect to one of those other pages or pass a variable to those other pages?

As for clicking I assume you mean a link with a value passed in the URL. Just do it like this:

<a href="yourpage.php?value=t2">Click here</a>

And then in your PHP code:




<?php

$value = $_GET['value'];

switch($value) {
case "t2":
echo "t2";
break;
case "t3":
echo "t3";
break;
}

?>

grudz
01-22-2004, 09:38 PM
thanx for the advice, i'll certainly remember that......anyway....the thing is this:

i have a table in my database that holds all my information, a listing (picture a yellow pages of some sort, but on the internet). i have one page: list.php that shows all the records (the company name, address, type, location, etc....)depending on other pages.....for example:

all my records show on list.php, when a user clicks on a record, they go to establishment.php/?name=value(whatever it is)....then in my establishment.php page, i have a button that says "click here for more on this street"....what i want that to do is go back to list.php, but this time i want the records to show depending on the address and type.

basically, im going to have alot of those types of pages that refer back to list.php, but with different variables.

my link (http://www.mtl-baseline.com/list.php?type=lounge)

here is the site im working on right now.....the page ur on is list.php and it's showing me the type "lounge" thats great and it works perfectly, now click on "fontaine" ...thats my establishment.php.....now there isnt the button that's going to refer the user back to list.php, but thats what i want......plus like i said, there will be alot of different pages that'll refer back to list.php....

do you understand? im sorry if it's not that clear.......i want alot of pages that use list.php as there search results page?

thanx again...i really appreciate it (ive been at this for 2 whole weeks....no joke)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum