Displaying SQL data as forms value attribute

Associate
Joined
25 Feb 2007
Posts
905
Location
Midlands
Hey,

I have average knowledge of PHP/SQL. I'm trying to create a CMS system for a website I'm doing. I am currently able to add a new page with content and delete the page. I'm now working on the Edit Page part of it - I can it to update the required record in my SQL table with the contents of the form. What I'd like to do now, is display the CURRENT content in the form field using the 'value' attribute, so the user can edit any typos etc. rather than copying and pasting, or typing everything again.

I dont want to use a ready made CMS - sorry :D

Can anyone help?

Cheers,
 
Yeah that's what I want, although it isn't working. Is $row a special variable? I'm currently using $result after performing my select query to pull the right data...

Any ideas?
 
It looks like this:

Code:
<input name="name" type="text" id="name" <?PHP 
      if ($HTTP_GET_VARS['action'] == 'edit'){
      $pageID = $HTTP_GET_VARS['id'];
      $result = $connector->query('SELECT ID,name FROM pages WHERE id='.$pageID);
      echo 'value="'.$result['name'].'">';
      }
     ?>

It this right?

Cheers,
 
OK, similar question. How would I do the same thing for a select list? At the moment I have a list which is being built by PHP (fetching the page id's and names in a loop) for the 'parent page'. How could I pull this value from my table as I have asked above (for the same reason)?

Thanks,
 

This is what I have already, the list is already being built using PHP looping through the pages table. What I want to do is show the current value for the page being edited, as I have done above for text fields using an SQL query for the value attribute.
 
You'll need to use the keyword 'Selected' for the appropriate <option>, and have an if statement to select the right value, i.e:

PHP:
for(blah, blah, blah) {

	$idfromdb = mysql_result($result,$i,"id");

	if ($currentid == $idfromdb) {
			echo "<option value='$someval' selected>Some Text</option>";
		}
		else {
			echo "<option value='$someval'>Some Text</option>";
		}
	}


}

OK, I've done this for a list that displays a list of pages - this is fine as all the information is in one table. I have a field in the pages table which uses the ID from another table (slideshows). How would I put the selected attribute by the chosen option for this list?

Sorry it doesn't make much sense.

Cheers.
 
Right, so i've got two tables - 'pages' and 'slideshows'. Pages holds any content for the page and also an ID for 'Parent Page' and 'Slideshow' if it has one.

Parent Page and Slideshow are both selected from a drop down list generated by PHP. This loops through the relevant table, populating the list 'options' with the ID and name from each table.

Code:
<select name="parent">
<option value="0">None</option>
<?PHP
$result = $connector->query('SELECT ID,name,parent FROM pages ORDER BY name');
while ($row = $connector->fetchArray($result)){
if ($HTTP_GET_VARS['action'] == 'edit'){
echo '<option value="'.$row['ID'].'">'.$row['name'].'</option>';}
}
}
?>
</select>

This is fine for creating a new page, however, when editing a page, I want the information already in the table to be displayed. I've achieved this for the text boxes using the value attribute and an SQL query:

Code:
<input name="name" type="text" id="name" <?PHP 
if ($HTTP_GET_VARS['action'] == 'edit'){
$pageID = $HTTP_GET_VARS['id'];
$result = $connector->query('SELECT ID,name FROM pages WHERE id='.$pageID);
$row = $connector->fetchArray($result);
echo 'value="'.$row['name'].'">';
}
?>

This looks at a http variable called 'id' and checks the row for that id, pulling the relevant field of that row.

I want to be able to do the same for the list boxes, using the selected attribute. Sorry if this is a bit heavy. Any help?

Cheers,
 
Back
Top Bottom