Results 1 to 9 of 9

Thread: [Resolved]PHP Help

  1. #1

    Thread Starter
    Lively Member Rivkah's Avatar
    Join Date
    Jun 2010
    Location
    192.168.1.16
    Posts
    93

    Resolved [Resolved]PHP Help

    Ok, wasn't sure on how to even search this so sorry if this has already been recently covered.
    My problem: I am trying to get it to display my results from MySQL into a combobox.
    I know the while part works fine because I use it to display what is from the MySQL earlier in the page..Here is the code that is giving me problems (No errors report on the page).
    What I want it to do is to use the option value as the id of the MySQL entry and the name of it as the name in the table.
    PHP Code:
    <?php while ($row $connector->fetchArray($result)) {echo '<option value="'.$row['ID'].'">'.$row['name'].'</option>';} ?>
    Last edited by Rivkah; Feb 1st, 2011 at 05:24 PM. Reason: Resolved

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: PHP Help

    so, what's the issue? do you get an error? does it not work as expected, but sort of work?

    you're using some non-standard object-oriented method to get data from the database, so there's no real way to tell if that's working properly with what you've posted. the logic of what you've posted looks fine, though. make sure your database field is named "ID" rather than "id," though, because associative arrays in PHP are case sensitive.
    Like Archer? Check out some Sterling Archer quotes.

  3. #3

    Thread Starter
    Lively Member Rivkah's Avatar
    Join Date
    Jun 2010
    Location
    192.168.1.16
    Posts
    93

    Re: PHP Help

    It is, and my problem is (sorry for forgetting to post it) that it won't show it in the combobox. It just doesn't show anything.
    Added a ScreenShot of it, it is suppose to add in those section names/IDs from MySQL to that ComboBox..Oh and P.S. Yea, its probably old and funky..but was using: http://www.intranetjournal.com/artic...10_08_04b.html as a basis, but I have been researching it all and updating things to what seems more appropriate.
    Well, I've continued trying and can't even get:
    PHP Code:
    <?php echo '<option value="lol">'.$row['name'].'</option>'?>
    to work...Is this just bad/outdated form?:
    PHP Code:
    <?php
    // Require the classes
    require_once('../includes/DbConnector.php');
    require_once(
    '../includes/Validator.php');

    // Create an object (instance) of the DbConnector and Validator
    $connector = new DbConnector();
    $validator = new Validator();

    if (
    $_GET['action'] == 'delete'){

        
    // Store the ID of the section to be deleted in a variable
        
    $sectionID $_GET['id'];

        
    // Validate the section ID, and if it's ok then delete the section
        
    if ( $validator->validateNumber($sectionID,'Section ID') ){

            
    // The validator returned true, so go ahead and delete the section
            
    $connector->query('DELETE FROM cmssections WHERE ID = '.$sectionID);
            echo 
    'Section Deleted <br><br>';

        }else{

            
    // The validator returned false, meaning there was a problem
            
    echo "Couldn't delete. There was a problem with: ".$validator->listErrors();

        }

    }
    // Execute the query to retrieve articles
    $result $connector->query('SELECT ID,name,parentid FROM cmssections');

    // Get an array containing the results.
    // Loop for each item in that array
    while ($row $connector->fetchArray($result)){

    echo 
    $row['name'].' - &nbsp;&nbsp; '// Show the name of section
    echo '<a href="editSections.php?action=delete&id='.$row['ID'].'"> Delete </a>'// Show the delete link 
    echo '<br>'// Show a carriage return

    }
    ?>
    <html>
        <body>
            <form name="form1" method="GET" action="editSections.php">
                <p>Section Name:
                    <input name="title" type="text" id="title">
                </p>
                <label for="style">Parent Section:</label>
                <select name="style">
                    <option value="">No Parent</option>
                    <?php echo '<option value="lol">'.$row['name'].'</option>'?>
                </select>        
                </p>
                <p>
                <input type="submit" name="submit" value="Create Section">
                </p>
            </form>
            <br />
            <br />
            <a href="..">Back to Home Page</a>
        </body>
    </html>
    Attached Images Attached Images  
    Last edited by Rivkah; Jan 30th, 2011 at 07:59 PM.

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: PHP Help

    if your echo statement isn't even printing out, then something might be going on. did you actually check the source of the HTML file to make sure it wasn't being output at all? $row['name'] may not have a value in this case.
    Like Archer? Check out some Sterling Archer quotes.

  5. #5

    Thread Starter
    Lively Member Rivkah's Avatar
    Join Date
    Jun 2010
    Location
    192.168.1.16
    Posts
    93

    Re: PHP Help

    It does, its printing out the line:
    PHP Code:
     echo $row['name'].' - &nbsp;&nbsp; '// Show the name of section 
    Thats where its showing Section 2 and Section 1.1 on the ScreenShot, its looping through to show all the Sections..want it to do that with the ComboBox
    Can it only loop through once or something?
    Last edited by Rivkah; Jan 31st, 2011 at 07:14 AM.

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: PHP Help

    1) Yes you are correct, you have to rewind the cursor back to the beginning (I used the term rewind on purpose...hint.)
    2) Even if you managed to rewind the cursor, it wouldn't matter as the code currently does not have the option tag in a loop.

    Here's how I'd tackle it - get the data into an array... outside of any loops... then loop through the array, do the first printing... then do a second loop through it for your drop down. But the key is to call fetcharray once. I'd probably then do the loops as a foreach instead of a while.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Lively Member Rivkah's Avatar
    Join Date
    Jun 2010
    Location
    192.168.1.16
    Posts
    93

    Re: PHP Help

    Quote Originally Posted by techgnome View Post
    1) Yes you are correct, you have to rewind the cursor back to the beginning (I used the term rewind on purpose...hint.)
    2) Even if you managed to rewind the cursor, it wouldn't matter as the code currently does not have the option tag in a loop.

    Here's how I'd tackle it - get the data into an array... outside of any loops... then loop through the array, do the first printing... then do a second loop through it for your drop down. But the key is to call fetcharray once. I'd probably then do the loops as a foreach instead of a while.

    -tg
    Sorry I haven't gotten a chance to search this yet but todays been kinda busy, but anyways..Not sure exactly what you mean. Sorry, not very literate in PHP outside of connecting to/selecting from the DB and echoing or creating an image this info..If you could elaborate more on this it would be great, if not I'll check it out tomorrow when I get a chance

  8. #8
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: PHP Help

    he was, as far as I know, saying that since you want to display the same records twice, it would be more efficient to store the records you return in an array and display the array twice. otherwise, you're putting unnecessary strain on your database. so, you could theoretically do something like this, using plain old MySQL functions:
    PHP Code:
    // The section array
    $sections = array();

    // Query the database
    $sql "SELECT id, name, number FROM sections";
    $query mysql_query($sql);
    while(
    $row mysql_fetch_assoc($query)){
      
    // Store this data
      
    $sections[] = $row;
    }

    // Now, I have all of the data stored in $sections, so I can iterate through it
    // whenever I want to.

    // Loop through:
    foreach($sections as $section){
      
    print_r($section);
      
    // prints out the id, name, and number
    }

    // And again, with a different type of loop
    for($i 0$i count($sections); $i++){
      
    print_r($sections[$i]);
      
    // prints the same

    make any sense?
    Like Archer? Check out some Sterling Archer quotes.

  9. #9

    Thread Starter
    Lively Member Rivkah's Avatar
    Join Date
    Jun 2010
    Location
    192.168.1.16
    Posts
    93

    Re: PHP Help

    Ah, looks like what I'm looking for ..Thanks, will test it out with my code once I get back to my computer later.

    Final Edit: Thank you so much kows, it works flawlessly
    Attached Images Attached Images  
    Last edited by Rivkah; Feb 1st, 2011 at 05:23 PM.

Posting Permissions

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



Click Here to Expand Forum to Full Width