SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using PHP to run different queries within one page depending on a variable value

    Hey guys I am creating a user account page where the user can access their account details from the users table, I want to have a few links on the account page that will allow them to do certain things with their information, such as view, change, edit password. So I was trying to make these options links that returned the same page but clicking a link sets a php variable to say 1, 2 or 3 and I have 3 queries written out and and if statement setting a condition if variable = 1 then run and display this query, or another etc...

    I was doing this by creating a link like this:
    Code:
    <li><a href='myaccount.php?userinfo=1'>View account info</a></li>
    At the top of the page I have the variable set to 0 $userinfo = 0;

    I cannot get this to work, can anyone tell me what I am doing wrong? The idea is that I can return the information from the table depending on what link the user clicks on.

    I would appreciate any assistance with this.

    Many thanks.

  2. #2
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Your idea is OK, and so is the code youve posted, so I suspect the problem will be with your filering code that delivers the required content

    It should look something like

    PHP Code:
    $choice $_GET['userinfo'];

    if (
    $choice == 1){
    // your code 
    }

    if (
    $choice == 2){
    // your code
    }

    if (
    $choice == 3){
    // your code

    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your advice, it makes sense, I cannot figure out how t use your advice. Here is my code, can you see where I am masking my mistake?


    Code:
    <?php	
    $userinfo = 0;
    
    $choice = $_GET['userinfo'];
    
    
    	if (isset($_SESSION['firstname'])){
    									
    
    
    
    echo'hello ' . $user_name;}
    
    else{
    
    echo 'You have logged out';}
    
    
    echo "<ul class='sublink'>
    				<li><a href='myaccount.php?choice=1'>Shirts</a></li>
    				<li><a href='myaccount.php?choice=0'>Jackets</a></li>
    				<li><a href='products.php?category=men&range=shorts'>Shorts</a></li>
    				<li><a href='products.php?category=men&range=hat'>Hats</a></li>
    			</ul>";
    
    
    			
    			
    if ($choice == 1){
    			
    	$query = "select * from cryptuser WHERE userID ='" . $userid ."'";						
    										
    		$result2 = sqlsrv_query( $conn, $query, array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));
        
    		if( $result2 === false)
    		{
    			 echo "Error in query preparation/execution.\n";
    			 die( print_r( sqlsrv_errors(), true));
    		}
        
    			 while( $row = sqlsrv_fetch_array ( $result2, SQLSRV_FETCH_ASSOC))
    		{
    			echo '<p id="productsContent">'.$row['firstname'];
    			echo '<br/>'.$row['surname'];
    			echo '<br/>'.$row['email'];
    			echo '<br/>'.$row['address'];
    			echo '<br/><br/><a href="products.php?productID='.$row['productID'].'"><img id="searchimage" src="' . $row['picture'] .'" /></a>';
    			echo '<br/><br/><p/>';
    		}
    
    
    
    }
    			
    
    
    ?>
    Many thanks!

  4. #4
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Put this into myaccount.php and run it, hopefully you'll see how it works

    PHP Code:
    <?php 
    $choice 
    $_GET['userinfo'];
    ?>
    <ul class='sublink'>
     <li><a href='myaccount.php?userinfo=0'>Shirts</a></li>
     <li><a href='myaccount.php?userinfo=1'>Jackets</a></li>
     <li><a href='myaccount.php?userinfo=2'>Shorts</a></li>
     <li><a href='myaccount.php?userinfo=3'>Hats</a></li>
    </ul>

       
    <?php   
    if ($choice == 0){
     echo 
    '<h1> You chose Shirts </h1>';
    }
    if (
    $choice == 1){
     echo 
    '<h1> You chose Jackets</h1>';
    }
    if (
    $choice == 2){
     echo 
    '<h1> You chose Shorts</h1>';
    }
    if (
    $choice == 3){
     echo 
    '<h1> You chose Hats</h1>';
    }
       

    ?>
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mandes, that worked perfectly, so I implemented it into my code and it didn't work, I tried forever and couldn't get additional PHP code to fit in with it. Here is my complete script, can you see how this doesn't work at all?

    <?php
    $choice = $_GET['userinfo'];
    ?>

    <?php
    if (isset($_SESSION['firstname'])){

    echo'hello ' . $user_name;}

    else{

    echo 'You have logged out';}
    ?>



    <ul class='sublink'>
    <li><a href='myaccount.php?choice=0'>one</a></li>
    <li><a href='myaccount.php?choice=1'>two</a></li>
    <li><a href='products.php?choice=2'>three</a></li>
    <li><a href='products.php?choice=3'>four</a></li>
    </ul>"





    <?php
    if ($choice == 0){
    echo '<h1> You chose one </h1>';


    $query = "select * from cryptuser WHERE userID ='" . $userid ."'";

    $result2 = sqlsrv_query( $conn, $query, array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));

    if( $result2 === false)
    {
    echo "Error in query preparation/execution.\n";
    die( print_r( sqlsrv_errors(), true));
    }

    while( $row = sqlsrv_fetch_array ( $result2, SQLSRV_FETCH_ASSOC))
    {
    echo '<p id="productsContent">'.$row['firstname'];
    echo '<br/>'.$row['surname'];
    echo '<br/>'.$row['email'];
    echo '<br/>'.$row['address'];
    echo '<br/><br/><a href="products.php?productID='.$row['productID'].'"><img id="searchimage" src="' . $row['picture'] .'" /></a>';
    echo '<br/><br/><p/>';
    }
    }


    if ($choice == 1){
    echo '<h1> You chose two </h1>';
    }



    ?>

  6. #6
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Are you sure that youre using MS SQL Server ? or are you using mySQL ?
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  7. #7
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am using SQL Server 2008 R2

    Locally using ISS and windows 7.

  8. #8
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I was doing too many things first time round

    Youve changed my script from

    PHP Code:
    <ul class='sublink'>
     <
    li><a href='myaccount.php?userinfo=0'>Shirts</a></li>
     <
    li><a href='myaccount.php?userinfo=1'>Jackets</a></li>
     <
    li><a href='myaccount.php?userinfo=2'>Shorts</a></li>
     <
    li><a href='myaccount.php?userinfo=3'>Hats</a></li>
    </
    ul
    to

    PHP Code:
    <ul class='sublink'>
    <
    li><a href='myaccount.php?choice=0'>one</a></li>
    <
    li><a href='myaccount.php?choice=1'>two</a></li>
    <
    li><a href='products.php?choice=2'>three</a></li>
    <
    li><a href='products.php?choice=3'>four</a></li>
    </
    ul>
    I was seting a URL variable called 'userinfo', you are calling it 'choice'
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  9. #9
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mandes, I am sorry, I have to take a break, I made a mistake. Your code works fine, thank you for all your help I appreciate it. I am making silly mistakes now


    You solved my problem!



    Cheers!

  10. #10
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    no problem
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming


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
  •