Jump to content

PHP Learner

Members
  • Posts

    37
  • Joined

  • Last visited

Posts posted by PHP Learner

  1. Thanks very much, but, the guy that wrote the above code saysabout the rand()..

     

    "The problem with this method is that it is very slow. The reason for it being so slow is that MySQL creates a temporary table with all the result rows and assigns each one of them a random sorting index."

     

    My table is gonna become massive, with thousands of entries so I chose this method to overcome snags in the future. Not sure if this is correct but others who commented on the page seem to agree.  Is there anyway of adapting the code to counter repeating entries? Or something you can do with PHP?

     

    Cheers, pLate

  2. Hello coders,

     

    I'm using this code to grab random results from a table and it works well but I need it to be a non-repeating event.

     

    $offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `jobs_board` ");
    $offset_row = mysql_fetch_object( $offset_result );
    $offset = $offset_row->offset;

     

    I'm using this query followed by the html which is a set of divs with inline echo statements for the various variables I've localised from the table. Then I'm repeating that process a number of times to create a small list (I wanted to use a single while loop but cant figure it out at the moment).  Anways, how can I make it so each query is random but also not repeated? At the moment they are indeed random but sometimes one same entry will appear a number of times consecutively...

     

    Nice one guys, L-plate

  3. Hello everyone,

     

    I'm trying to code a section of the site which is like an advertising website (i.e, gumtree) where people can post an Ad on the site and have any responses sent to them via email. The thing is that I have a form where they can enter their details and need to POST the information to the following page which is a preview page of their Ad. I have the form action returning to the same page for validation but I need the $_POST vaules to populate the following page without sending information to the database (in an effort to keep the database clean should they decide not to publish their Ad.

     

    There are 3 pages in total CREATE AD > PREVIEW AD (where they can go back and edit if they need to) > Publish AD (where they confirm terms and send to database).  I do have session variables already set after login in and a seperate table to hold the details after they have confirmed the Ad & terms.

     

    Any ideas would be greatly appreciated.

     

    Thanks, L-plate

  4. Ok update, I came back with a clear head and wrote it again.  It's only marginally different to the code you wrote mikesta but it is working.  It could either be that I used Capitals or the apostraphes are the other way round perhaps, either way it's DONE lol.  Just so you can see, the code I put was as follows...

     

    	<select name="gender" id="gender">
                  <option value="" <?php if (!isset($gender)) {echo 'selected="selected"';} else {echo "";} ;?>>Select</option>
                  <option value="Male" <?php if ($gender == "Male") {echo 'selected="selected"';} ;?>>Male</option>
                  <option value="Female" <?php if ($gender == "Female") {echo 'selected="selected"';} ;?>>Female</option>
                </select>
    

     

    As for the next part of the conundrum if that's how you spell it.  If the selected item is submitted twice I get a blank screen?!?!? Any thoughts, anybody? I think I need to find a way to cancel the update to the database if it's gonna do that.  The other thing still is the multiple dropdowns, if I make selections on more than one menu (say the second was a date (day) seletor and the third a month selector I get the blank screen again. 

     

    Appreciate the help guys, Learner

  5. Thanks for the effort mikesta but the problem still remains, I copied the code and planted it straight into the page but it still returns 'Select' as the selected option :-\.  It appears (the way DW writes it anyway) that selected="selected" is the way it defines it although even if you are indeed correct the result is the same.  With the way it's written, the selected value should only appear in one of the <options> right? either there is nothing in the DB for it to  echo and thus Select is used or it's either Male or Female, but alas no joy.

     

    Thanks again, Learner

  6. Well thanks for your help so far, indeed echoing the variables back is a good idea to check that they have the value from the database or $_POST after the form is submitted (which they do).  Not sure I understand your example madjack, but thanks yeah.  I got the relivent pieces of the code together and thought I'd post it here so you can see.

     

    
    <?php session_start(); ?>
    
    <?php if (isset($_SESSION['id']))
    
    $id = $_SESSION['id'];
    $email = $_SESSION['email'];
    
    // Connect to database
        include("../includes/connect.php");
    
    // Select Database
    $sql = mysql_query("SELECT gender FROM members WHERE id='$id' AND email='$email'");
    
    if (!$sql) {
    die(mysql_error());
    }
    
    // Use returned data
    while ($row = mysql_fetch_array($sql)) {
    $gender = $row['gender'];
    }
    
    
    //Get the POST results from the form 
    if (isset($_POST['submit'])) {
    $gender = $_POST['gender'];
    }
    
    // Add user info into the database table for the main site table
    
    mysql_query("UPDATE members SET gender='$gender' WHERE id='$id' AND email='$email' LIMIT 1");
    
    
    if (!mysql_affected_rows() == 1) {
    die(mysql_error());
    }
    
    
    } // Close if (isset($_POST['submit']))
    
    ?>
    
    <html>
    <body>
    
    <form action="same_page.php" method="post" enctype="multipart/form-data">
    
        <select name="gender" id="gender">
                  <option selected="selected">Please Select</option>
                  <option value="Male">Male</option>
                  <option value="Female">Female</option>
                </select>
                
    <input type="submit" name="submit" id="profile_submit" value=" Save and Update! " />
    
    </form>
    
    
    <?php echo "{$gender}" ;?>
    
    </body>
    </html>
    
    

     

    See now this works fine, it returns data from the database or from the POST but each time the page is refreshed the "selected" option (or default) is set to 'Please Select' instead of using the new value from the database.

     

    I have also tried putting

    <?php echo "{$gender}" ;?>

    in the value and/or the selectable output text which also works, like so ->>

    
    <form action="same_page.php" method="post" enctype="multipart/form-data">
    
        <select name="gender" id="gender">
                  <option value"<?php echo "{$gender}" ;?>"><?php echo "{$gender}" ;?></option>
                  <option value="Male">Male</option>
                  <option value="Female">Female</option>
                </select>
    
                
    <input type="submit" name="submit" id="profile_submit" value=" Save and Update! " />
    
    </form>
    
    

     

    This does grab the data from the member row and appear as the value previously submitted, but, if you go to change the selection to something else when you click the dropdown you have 2 entries which are the same.  I.e, if Male was selected it would show Male twice and Female once.  I hope you understand what I mean, if not I'll post a .GIF

     

    Actually here it is anyway :shrug:

     

    6136271569

     

    Ok so I dont know how to print the img here but it's attached, up in the top right corner is the echo return variable from the datebase. All the other things I posted before were my attempts to get around this seemingly easy problem, LOL.

     

    Cheers guys, Learner

     

    [attachment deleted by admin]

  7. Hello everyone,

     

    So what I'm trying to do is have a dropdown menu displaying a number of <options> for people to select and to update that selection to the database, easy enough right? But I want that option to be displayed as the "selected" option when the page is revisited or refreshed and I just can't figure it out!!! (Permission to bang head on desk?) It would seem like it sould be a really basic thing to do but it's got me completely and a lot of menus around the site are going to rely on this so I came to you guys for help.

     

    A simple example would be like the facebook edit profile page, the user selects whether they are Male or Female, the database gets updated and when you return the option you selected before is the one that appears as if selected="selected" had been done. I've tried everything I can think of (all be it from a learners perspective) with no joy, ive managed to get the database connection sorted, the tables done, the login with unique id $_SESSION, logout etc... so then when I got to this I thought... easy LOL yeah right.

     

    Some of this probably doesnt even make sense but I'll show you the kind of things I've tried...

     

     

                <select name="gender" size="1" id="gender">

                  <option value="male" <?php if ($gender == "male") {echo 'selected="selected"';} ;?>>Male</option>

                  <option value="female" <?php if ($gender == "female") {echo 'selected="selected"';} ;?>>Female</option>

                </select>

     

     

     

    OR

     

     

        <select name="gender" id="gender">

                  <option value="" selected="<?php if (!isset($gender)) {echo "selected";} ;?>">Select</option>

                  <option value="male" selected="<?php if ($gender == "male") {echo "selected";} else {echo "";} ;?>">Male</option>

                  <option value="female" selected="<?php if ($gender == "female") {echo "selected";} else {echo "";} ;?>">Female</option>

                </select>

     

     

    OR

     

     

     

                <select name="gender" size="1" id="gender">

                  <option selected="<?php if (!isset($gender)) {echo "selected";} ;?>">Select</option>

                  <option value="<?php if ($gender == "Male") {echo "selected";} else {echo "male";} ;?>">Male</option>

                  <option value="<?php if ($gender == "Female") {echo "selected";} else {echo "female";} ;?>">Female</option>

                </select>

     

     

    OR

     

                <select name="gender" id="gender">

                  <option value="male"><?php if ($gender == "male") {echo "Male";} ;?></option>

                  <option value="female"><?php if ($gender == "female") {echo "Female";} ;?></option>

                </select>

     

     

    Honestly man, I've got no idea.

     

     

    The other thing is, I have more than 1 dropdown menu in the same form (5 in total) and if I use 2 or more selecting different options as I go I get a blank screen.  And one more, if I have selected Male and it updates the users row and I resubmit Male again it's blank screen time again, lol.

     

    Any help would be tremendous and greatly appreciated.

     

    Thanks very much, Learner

     

    P.S  :wtf: Man!

     

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.