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
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.
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
// 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'].' - '; // 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
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.
echo $row['name'].' - '; // 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.
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.
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
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 }