SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Using Link Data

  1. #1
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using Link Data

    Hi all,

    I have numerous links, each with unique values. For simplicity purposes I'll use two links...(each taken from separate queries.)
    PHP Code:
    <a href="view_files.php?ida='.$array['id'].'">'.$array['name'].'</a>
     <
    a href="view_files.php?idb='.$array['id'].'">'.$array['name'].'</a
    When a certain link is selected the relevant code from the switch statement should be used.
    However, the first one is always used. Why is this?

    PHP Code:
     if(!empty($_GET))
     {
     
    // Connect to database
     
    include('../../../db_connect.php');
         
     
    // declare variables 
     
    $ida $_GET['ida'];
     
    $idb $_GET['idb'];
     
     
    // check which file selected
     
    switch(TRUE)
     {
     case (
    '$ida'):
         
    $query   "SELECT name, type, size, content ".
                  
    "FROM mergers ".
                  
    "WHERE id = '$ida'";
         echo 
    $query;
         break;
             
     case (
    '$idb'):
         
    $query   "SELECT name, type, size, content ".
                  
    "FROM centralised_archive.mergers ".
                  
    "WHERE id = '$idb'";
         echo 
    $query;
         break;
     }
     
     
    $result  mysql_query($query) or die(mysql_error()); 

  2. #2
    SitePoint Addict
    Join Date
    Nov 2005
    Location
    Germany
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try
    PHP Code:
    echo '$ida'
    The variable won't be evaluated within single quote strings. And as a non-empty string it is true.

  3. #3
    SitePoint Wizard
    Join Date
    Jan 2004
    Location
    3rd rock from the sun
    Posts
    1,005
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Switch generally analyses one variable, if its not true it goes to the next one...

    If you are intent on using a switch (you may have your reasons) then I would set something in the url:

    t for "type", v for value;

    <a href="view_files.php?t=1v='.$array['id'].'">'.$array['name'].'</a>

    ....


    $v=(int)$_GET['v']; //make sure its an int!


    Then analyse $t in your switch:

    PHP Code:
    switch((int)$_GET['t'])  //make sure its an int!
    {
    case (
    1):
        
    $query   "SELECT name, type, size, content ".
                 
    "FROM mergers ".
                 
    "WHERE id = '$v'";
        echo 
    $query;
        break;
            
    case (
    2):
        
    $query   "SELECT name, type, size, content ".
                 
    "FROM centralised_archive.mergers ".
                 
    "WHERE id = '$v'";
        echo 
    $query;
        break;

    Then do a "default" if the no. isnt recognised

    Switch generally analyses one variable, if its not true it goes to the next one... So its checking to see if $t==1 or goes off to next one, and so on till it gets to the default.

    Paul

    EDIT

    If its a monster switch you might want to isolate the bit thats changing:
    PHP Code:

    case (1):
          
    $querybit  "FROM mergers ";
          break;
    case (
    2):
        
    $querybit   "FROM centralised_archive.mergers ";
        break;
    }

    $query "SELECT name, type, size, content ".
    $query.=$querybit;
    $query.="WHERE id = '$v'";

    echo 
    $query
    Hope that helps you...

  4. #4
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi paulyg. Will $t always be set to 1 for each link??

  5. #5
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    Hi Kev,

    Just how I would approach it....
    PHP Code:
    if(!empty($_GET))
    {
    // Connect to database
    include('../../../db_connect.php');
        
    // declare variables 
    $var = isset($_GET['ida']) ? $_GET['ida'] : $_GET['idb'];
    // check which file selected
    switch($var)
    {
    case (@
    $_GET['ida']):
        
    $query_table   " mergers ";
        
    $var $_GET['ida'];
        break;
            
    case (@
    $_GET['idb']):
        
    $query_table   " centralised_archive.mergers ";
        
    $var $_GET['idb'];  
        break;
    }

    $query "SELECT name, type, size, content ".
                 
    "FROM "$query_table ."
                 WHERE id = "
    $var;
    #$result  = mysql_query($query) or die(mysql_error());
    echo $query;

    Mike

  6. #6
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Mike,

    So if I have 3 or 4 variables would they be set as...

    PHP Code:
    $var = isset($_GET['ida']) ? $_GET['ida'] : $_GET['idb'] ? $_GET['id2a'] : $_GET['id2b'] ? $_GET['id3']; 


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
  •