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
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    77
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Else statement if Fetch returns no results

    Hello everyone,

    Hope you're all well.

    Below i'm obtaining data from the database to display inside a <select> form which is basically a profile which displays the profile name. What I want to do is if a profile does not exist to do an ELSE statement... i've tried a few ways but have not no success... how could I do this?

    Currently, if the data cannot be obtained the form doesn't appear...

    If anyone could assist, that would be appreciated.


    PHP Code:
    $username=$_SESSION['username'];
    $query "SELECT * FROM individual_profile WHERE username = '$username'";
    $propertyid $_SESSION["propertyid"];

    if (
    $query_run mysql_query($query))
    {
        while (
    $query_row mysql_fetch_assoc($query_run))
        {
            
    $fname $query_row['fname'];
            
    $lname $query_row['lname'];
            
    echo 
    '<div class="agent"> Seller:
      <select name="seller" class="agentlist" tabindex="2">
        <option value="'
    .$fname.'"'.$lname.'">'.$fname.' '.$lname.'</option></select></div></div></form>';
        
            }

    }
    // Else echo 'Please create a profile';
    ?> 

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,159
    Thanks
    23
    Thanked 599 Times in 598 Posts
    Do the check before the query to save hitting the DB when you don't need to:
    PHP Code:
    <?php
    session_start
    (); //IT'S IMPORTANT TO HAVE THIS HERE
    if(isset($_SESSION['username'])){
        
    $username=$_SESSION['username'];
        
    $query "SELECT * FROM individual_profile WHERE username = '$username'";
        
    $propertyid $_SESSION["propertyid"]; //AND EVERYTHING THAT NEEDS TO BE DONE - PUT HERE

    }else{
        echo 
    'Please create a profile';
    }
    ?>
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    New Coder
    Join Date
    Oct 2011
    Posts
    77
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sunfighter View Post
    Do the check before the query to save hitting the DB when you don't need to:
    PHP Code:
    <?php
    session_start
    (); //IT'S IMPORTANT TO HAVE THIS HERE
    if(isset($_SESSION['username'])){
        
    $username=$_SESSION['username'];
        
    $query "SELECT * FROM individual_profile WHERE username = '$username'";
        
    $propertyid $_SESSION["propertyid"]; //AND EVERYTHING THAT NEEDS TO BE DONE - PUT HERE

    }else{
        echo 
    'Please create a profile';
    }
    ?>
    Thank you, however this won't work as to get to this page the session will already be set...

    The profile information I am trying to access is created via a different page... hence why I am trying to complete an echo if it turns no results as I don't really have anything to do a isset function...

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,159
    Thanks
    23
    Thanked 599 Times in 598 Posts
    Quote Originally Posted by rexhvn View Post
    Thank you, however this won't work as to get to this page the session will already be set...

    The profile information I am trying to access is created via a different page... hence why I am trying to complete an echo if it turns no results as I don't really have anything to do a isset function...
    Did you try this or just look and get weirded out by the session_start();?

    You need to have session_start(); as your first line in files if your going to use it.
    QUOTE: session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.
    HERE: http://kr1.php.net/session_start

    AND Yes "... the session will already be set... " They have to be.
    'isset' checks that they are. http://kr1.php.net/function.isset
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #5
    New Coder
    Join Date
    Oct 2011
    Posts
    77
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sunfighter View Post
    Did you try this or just look and get weirded out by the session_start();?

    You need to have session_start(); as your first line in files if your going to use it.
    QUOTE: session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.
    HERE: http://kr1.php.net/session_start

    AND Yes "... the session will already be set... " They have to be.
    'isset' checks that they are. http://kr1.php.net/function.isset
    I apologise... I think we have misunderstood each other?

    I already have session_start(); sorry I didn't include it in my above code...
    I have tried using if(isset($_SESSION['username'])) and it made no difference... I will try and re-explain what I was trying to achieve...

    Right now, if there is no profile created (via a different page) the form will not appear as I am trying to display the profile name in a SelectForm and there is nothing to return. However, when there is a profile created, it works completely fine...

    For a more user friendly environment... instead of the form not appearing at all, I wanted to have an else statement to prompt the user to create a profile. Because the session has already started far before this page, isset($_SESSION['username'] won't work.. hence why I was trying to see if I could do it a different way...

    I hope this makes a little more sense... sorry for the confusion...
    Last edited by rexhvn; 12-11-2013 at 08:17 AM.

  • #6
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,159
    Thanks
    23
    Thanked 599 Times in 598 Posts
    If you post the REAL code it should make even more sense.
    Right now your echo closes two divs and a form tag, but you only open one div.
    What your wanting is to have a select dropdown with a single option or with no options. Why?

    It appears that your listing items for sale and what the seller in the listings, but wouldn't be a buyer that is logged in and viewing so you never have a sessions var set for a seller.

    So I do need a little more info on what is being done. Total code wou;d help.

    FYI: Try not to mix HTML and PHP. You do that with the last echo statement.
    Another way of doing it would be:
    PHP Code:
     <?php
     session_start
    ();
     
    session_unset();
     
    // For Demo Only you can comment out to see results
     
    $_SESSION['fname'] = 'John';
     
    $_SESSION['lname'] = 'Smith';

    $fname = (isset($_SESSION['fname']))? $_SESSION['fname'] : '';
    $lname = (isset($_SESSION['lname']))?  $_SESSION['lname'] : '';
    $name $fname.' '.$lname;
    ?>

    <form>
        <div class="NOT NEEDED">
        <div class="agent"> Seller:
            <select name="seller" class="agentlist" tabindex="2">
                <option value="<?= $name ?>"><?= $name ?></option>
            </select>
        </div>
        </div>
    </form>
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #7
    New Coder
    Join Date
    Oct 2011
    Posts
    77
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sunfighter View Post
    If you post the REAL code it should make even more sense.
    Right now your echo closes two divs and a form tag, but you only open one div.
    What your wanting is to have a select dropdown with a single option or with no options. Why?

    It appears that your listing items for sale and what the seller in the listings, but wouldn't be a buyer that is logged in and viewing so you never have a sessions var set for a seller.

    So I do need a little more info on what is being done. Total code wou;d help.

    FYI: Try not to mix HTML and PHP. You do that with the last echo statement.
    Another way of doing it would be:
    PHP Code:
     <?php
     session_start
    ();
     
    session_unset();
     
    // For Demo Only you can comment out to see results
     
    $_SESSION['fname'] = 'John';
     
    $_SESSION['lname'] = 'Smith';

    $fname = (isset($_SESSION['fname']))? $_SESSION['fname'] : '';
    $lname = (isset($_SESSION['lname']))?  $_SESSION['lname'] : '';
    $name $fname.' '.$lname;
    ?>

    <form>
        <div class="NOT NEEDED">
        <div class="agent"> Seller:
            <select name="seller" class="agentlist" tabindex="2">
                <option value="<?= $name ?>"><?= $name ?></option>
            </select>
        </div>
        </div>
    </form>
    Thanks, I will post my code later tonight... I will also show some print screen examples too..

  • #8
    New Coder
    Join Date
    Oct 2011
    Posts
    77
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Below is my actual code. My whole concept relates to creating property listings to display on this website... this is just one page.

    With an agency for example, there are multiple agents hence why I have created a page to create profiles (sellers). The below page is one part of uploading a listing where an agent/individual will select who's details are to appear on this listing... make sense?

    My only issue is when there are no profiles created, the form does not appear so I wanted an else statement to display instead...

    PHP Code:
    <?php
    session_start
    ();

    include 
    'connect.php';

    $username=$_SESSION['username'];
    $query "SELECT * FROM individual_profile WHERE username = '$username'";
    $propertyid $_SESSION["propertyid"];

    if (
    $query_run mysql_query($query))
    {
        while (
    $query_row mysql_fetch_assoc($query_run))
        {
            
    $fname $query_row['fname'];
            
    $lname $query_row['lname'];
            
    echo 
    '<lu>About the listing</lu><form method="post" action="uploadpart2.php">

    <div class="propertytype" tabindex="1">Property Type:
      <select name="listingtype" class="salelist">
        <option value="House">House</option>
        <option value="Unit">Unit</option>
        <option value="Townhouse">Townhouse</option>
        <option value="Land">Land</option>
        <option value="Studio">Studio</option>
        <option value="Apartment">Apartment</option>
        <option value="Terrace">Terrace</option></select></div>

    <div class="agent"> Seller:
      <select name="seller" class="agentlist" tabindex="2">
        <option value="'
    .$fname.'"'.$lname.'">'.$fname.' '.$lname.'</option></select></div></div>'// This echo function isn't working properly just yet...
    ?>
    Last edited by rexhvn; 12-12-2013 at 09:33 AM.

  • #9
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,036
    Thanks
    2
    Thanked 316 Times in 308 Posts
    I think the reason no one here understands what/why you are doing this is because if the current visitor is registered and can be logged in at all (they have a session variable $_SESSION['username']), they WILL have a profile containing things like their first name and last name as that is something that would have been done at registration.

    Next, if the page you are showing in this thread requires someone to be logged in and to be of a user type: seller, you need to test for that before doing anything on the page, for both producing the form and processing that specific form. Each page that has limited access must test and enforce who can access it. It is a huge security hole to not perform the test, because anyone can reach your page, not just those you supplied a navigation link to.

    Lastly, you can use mysql_num_rows() to find out how many rows a successful SELECT query matched, but given the stated purpose of the code in this thread, I don't see how this will help. A registered/logged in user should have a profile that was created at registration. Perhaps you have too many separate steps making up this process?
    Last edited by CFMaBiSmAd; 12-12-2013 at 11:26 AM.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #10
    New Coder
    Join Date
    Oct 2011
    Posts
    77
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by CFMaBiSmAd View Post
    I think the reason no one here understands what/why you are doing this is because if the current visitor is registered and can be logged in at all (they have a session variable $_SESSION['username']), they WILL have a profile containing things like their first name and last name as that is something that would have been done at registration.

    Next, if the page you are showing in this thread requires someone to be logged in and to be of a user type: seller, you need to test for that before doing anything on the page, for both producing the form and processing that specific form. Each page that has limited access must test and enforce who can access it. It is a huge security hole to not perform the test, because anyone can reach your page, not just those you supplied a navigation link to.

    Lastly, you can use mysql_num_rows() to find out how many rows a successful SELECT query matched, but given the stated purpose of the code in this thread, I don't see how this will help. A registered/logged in user should have a profile that was created at registration. Perhaps you have too many separate steps making up this process?
    Hi CFMaBiSmAd, thank you for your input.

    There is a registration/login form that allows access to this platform... this is just one page within the platform.. Security for this has already been completed... the reason why I haven't completed a if(isset($_SESSION['username'] is because the user will have already logged in to gain access to this page (there would be no other way to gain access)

    I hope that made sense...

    So as far as I was concerned, security to determine if the user is signed in is irrelevant because the only way they can gain access to this page is to of signed in..

    My intention was to create profiles within the account. Being because there could be a number of sellers within an office. So they would use an office login... then have say 5 sellers within the office. When it comes time to create a listing, they would be able to select which seller's details would appear against the listing...

    Example:
    Office 1 ( John Smith enterprises )

    Sellers:
    John Smith,
    Andrew Smith
    Jo Smith etc

    So the above sellers would appear in the select form. At the moment it works... but i'm just running into two complications...

    1. The form will only appear if a profile is created...
    2. It only display one profile... not all.


  •  

    Posting Permissions

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