SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    england
    Posts
    328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Switch Statement help

    Hi
    can anyone see the problem with this bit of code for a switch statment
    here is the code i have tried several options but none seem to work.

    PHP Code:
     $query "select  url from site order by rand() limit 1"
    $res mysql_query($query);
    if (
    mysql_num_rows($res) == 0) {
    while (
    $res mysql_fetch_array($query)) {
    }
    $url $default_site;
    $siteid 0;
    } else {
    $url mysql_result($res0"url");

    switch (
    $res['url'])

    {

      case 
    "1.swf":

        
    $img "<img src='numbers/1.jpg'>";

        break;

      case 
    "2.swf":

        
    $img "<img src='numbers/2.jpeg'>";

        break;

      case 
    "3.swf":

        
    $img "<img src='numbers/3.jpg'>";

        break;



  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,260
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)
    What is
    $url
    and what is
    $res['url']

  3. #3
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,198
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    The switch looks ok, though you might want to replace $res['url'] with $url.

    In addition you need to look at your code at the top, as something is not correct there. I am not certain what exactly you are trying to do with the "while" but it can be removed.

    You might also want to look into using mysql_fetch_assoc instead of mysql_result or even better take a look on PDO or mysqli.

  4. #4
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    england
    Posts
    328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi
    i have changed the switch statement to this it works but i get 1.jpg all the time it doesn't change to 2.jpg like it should i can't see the problem
    PHP Code:
    switch ($url)

    {

      case 
    "1.swf":

        
    $img "<img src='numbers/1.jpg'>";

        break;

      case 
    "2.swf":

        
    $img "<img src='numbers/2.jpeg'>";

        break;

      case 
    "3.swf":

        
    $img "<img src='numbers/3.jpg'>";

        break;

      case 
    "Waiting":

        
    $img '<img src="smilies/waiting.gif">';

        break;


    ps the url is the field in the database where 1.swf ect our stored

  5. #5
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,198
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    In that case, you should check what the $url field contain. Most probably it does not contain what you expect it to be.

  6. #6
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    england
    Posts
    328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in the database field called url there are 1.swf and 2.swf, so everything looks fine i can't see the problem

  7. #7
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    england
    Posts
    328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi i have used this code it works fine but surley i dont have to do it this way.

    PHP Code:
    $query "select id, url from site order by rand() limit 1"
    $res mysql_query($query);
    if (
    mysql_num_rows($res) == 0) {
    $siteid 0;
    } else {
    $url mysql_result($res0"url");
    $siteid mysql_result($res0"id");




    switch (
    $url)

    {

      case 
    "1.swf":

        
    $img1 "<img src='numbers/1.jpg'>";

        break;

      case 
    "2.swf":

        
    $img2 "<img src='numbers/2.jpeg'>";

        break;

      case 
    "3.swf":

        
    $img3 "<img src='numbers/3.jpg'>";

        break;

     } 
    then echo $img1 $img2 and so on

  8. #8
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    // do this to look inside the variable and see what is being assessed
    var_dump$url );

    // You could try and do it this way
    $img '<img src="numbers/' . (int)$url '.jpg" />';

    echo 
    'URL ' $url ' creates image ' $img 
    anything incorrect should be assessed as 0.jpg

  9. #9
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    england
    Posts
    328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi cups i will try your code out later today let you know if it works
    thanks paul


Bookmarks

Posting Permissions

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