assiging variables on the fly

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
labert_the_old
Forum Newbie
Posts: 5
Joined: Fri Jun 13, 2003 7:22 pm

assiging variables on the fly

Post by labert_the_old »

Hi There,

Ive had no trouble creating the tables and displaying the unit number beside the edit and remove buttons. Im not sure how to assign a value to each of the buttons as there created. I need to know which unit has been choosen, for my edit/remove page. I was able to do it with a drop down menu using $_POST but in time if the database grows displaying the units in a table would be a nicer approach. Thanks for any help.

Sincerley,

Lambert

Code: Select all

<?php

function units_table()
{
  $unit_query = "SELECT unit_id FROM unit WHERE owner_id = '" . $_SESSION['owner_id'] . "' ";
  $unit_result = mysql_query($unit_query);
  //count is for assigning a value to a button?
  $count=0; 
  while($unit_entries = mysql_fetch_array($unit_result))
  {
  ?>
  <form name=owner_unit method=post action="room_editor.php">  
   
  <table width="24%" border="1" cellspacing="1" cellpadding="1">
    <tr>
      <td width="42%"><?php echo $unit_entries["unit_id"]; ?>&nbsp;</td>
    <td width="24%">
	<input type="hidden" name="unit_id" value="<? echo $unit_entries["unit_id"] ?>">
	<input type="submit" name="Submit3" value="Edit"></td>
    <td width="34%">
	<input type="hidden" name="unit_id" value="<? echo $unit_entries["unit_id"] ?>">
	<input type="submit" name="Submit4" value="Remove"></td>
  </tr>
  </table>
  
  </form>
  <?
  $count++;
  }   
   
}
?>
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post by Paddy »

Not sure what you are trying to do exactly. But if you are trying to create a table of items that a user has already selected in order for them to edit or remove items then why not make the listed items radio buttons with the unit_id as the value.
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post by cactus »

I've stared at this for what seems hours (yes I should be in bed, it's 02:37 GMT), but I can't see what the issue is.

From what I can see you are assigning values to buttons (the "hidden" input type, and you don't need 2 of them), I think the issues is how you are handling them on the form action.

I think you need to catch either Submit3 or Submit4 (which would be better named "edit" and "remove") in your "room_editor.php".

Other than that can you post more info, debug data etc?

Regards,
labert_the_old
Forum Newbie
Posts: 5
Joined: Fri Jun 13, 2003 7:22 pm

one way of doing it

Post by labert_the_old »

Hi there,

Ive figured out how to pass the varibles assigned to each button on to the next page. You simply have your button names named $count var each time there built. Next You read each of those names into a array. Then check the value of the array[$x] to see if it isset. Thats it. Thanks For your replies Im glad there people out there that are willing to help.

Lambert

Code: Select all

<?php
function units_table()
{
  //Grab the info
  $unit_query = "SELECT unit_id FROM unit WHERE owner_id = '" . $_SESSION['owner_id'] . "' ";
  $unit_result = mysql_query($unit_query);
  $edit_value=0; 
  
  //build the table with buttons with assigned values
  while($unit_entries = mysql_fetch_array($unit_result))
  {

  ?>
  <form name=owner_unit method=post action="">  
   
  <table width="24%" border="1" cellspacing="1" cellpadding="1">
    <tr>
      <td width="42%"><?php echo $unit_entries["unit_id"]; ?>&nbsp;</td>
    <td width="24%">
	<input type="submit" name="submit_edit<? echo "$edit_value" ?>" value="<? echo "Edit".$edit_value ?>"></td>
    <td width="34%">
	<input type="submit" name="submit_remove<? echo "$edit_value" ?>" value="Remove"></td>
  </tr>
  </table>
  
  </form>
  <?
  $edit_value++;
  }   
  //check to find which button was clicked on
  $size = $edit_value;
  $edit_value=0;
  //used for array which hold all the button info
  $edit_counter = 0;
   
	while($edit_counter<$size)
	{	
		$edit_button[$edit_counter] = "submit_edit" . $edit_value; 
		$remove_button[$edit_counter] = "submit_remove" . $edit_value;	
		//for simplisty sake I put the array values in to var_edit and var_remove to check in the $_post
		$var_edit = $edit_button[$edit_counter];
		$var_remove = $remove_button[$edit_counter];
		
		if (isset($_POST["$var_edit"]))
  		 {
     	  $_SESSION['editchoice'] = $edit_button[$edit_counter];
		  echo $_SESSION['editchoice'];
		  header("Location: room_editor.php?PHPSESSID=" . session_id());
    	 }  
	   
	    if (isset($_POST["$var_remove"]))
  		 {
     	  $_SESSION['removechoice'] = $remove_button[$edit_counter];
		  echo $_SESSION['removechoice'];
		  header("Location: room_editor.php?PHPSESSID=" . session_id());
    	 }  
	
	$edit_counter++;
	$edit_value++;
	
	}
	 
}
?>
:wink:
Post Reply