Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10

Thread: code inspection

  1. #1
    Regular Coder
    Join Date
    Jan 2004
    Posts
    245
    Thanks
    0
    Thanked 0 Times in 0 Posts

    code inspection

    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 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";
        break;
    case 
    "3";
        echo 
    "4";
        break;
    ?>
    why does it give me "Resource id #4 " instead of the "name" from my recordset?

    thanx

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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:



    PHP Code:

    echo $Recordset1['columnname']; 
    columnname would be whatever column is available from what you selected in your query. In your case it would be

    PHP Code:

    echo $Recordset1['name']; 
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Regular Coder
    Join Date
    Jan 2004
    Posts
    245
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok.....but i would understand if it returns "Recordset1" but it doesnt i shows me "Ressource id#4" whats that?

  • #4
    Regular Coder
    Join Date
    Jan 2004
    Posts
    245
    Thanks
    0
    Thanked 0 Times in 0 Posts
    alright so this would have to be the code?

    PHP 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

  • #5
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Looking at your switch statement it looks like you have a data type mismatch and a few typos going on.

    PHP Code:
    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
    PHP Code:
    switch(2) {
        case 
    2:
            echo 
    $Recordset1['name'];
            break;
        case 
    3:
            echo 
    "4";
            break;

    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #6
    Regular Coder
    Join Date
    Jan 2004
    Posts
    245
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
     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?

  • #7
    Regular Coder
    Join Date
    Jan 2004
    Posts
    245
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 Code:
     <?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

  • #8
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Originally posted by grudz
    PHP Code:
     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.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #9
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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 Code:
     <?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 Code:

    <?php  

    $value 
    $_GET['value'];

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

    ?>
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #10
    Regular Coder
    Join Date
    Jan 2004
    Posts
    245
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

    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)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •