Hi Guys,
This Post is in reference to the time I spent in order to write just a piece of code in PHP, though it would be very easy for most of the guys but if you are still learning then its a good thing to be proud of.
Ok let me explain a little about what I was trying to do and how I did it.
Requirement: I need to list down all the names of guys who falls under a certain category and display them on another page.
I can do this by creating static text box in the landing page and fetch the value from the database OR
I can dynamically generate the text box and display all the data there.
For that first call the sql query of the condition like this.
$sql = mysql_query("SELECT * FROM `list1` WHERE BLAH BLAH");
Now use the While query in order to get all the data required
<?php
while ($row = mysql_fetch_array($sql))
{
?>
<tr>
<td align="center" valign="middle" scope="row">
<input type="text" name="resname[<?php echo $row['id']; ?>]" id="resname[<?php echo $row['id']; ?>]" value="<?php echo $row['resname'];?>"> </td>
<td align="center" valign="middle" scope="row">
<input type="text" name="resname1[<?php echo $row['id']; ?>]" id="resname1[<?php echo $row['id']; ?>]" value="<?php echo $row['txt1'];?>"></td>
<td align="center" valign="middle" scope="row">
<input type="text" name="resname2[<?php echo $row['id']; ?>]" id="resname2[<?php echo $row['id']; ?>]" value="<?php echo $row['txt2'];?>"> </td>
</tr>
<?php
}
?>
The above piece of code is dynamically generating the text Box and the values are getting displayed on every text box.
Now the major problem comes in.......HOW WOULD YOU GET THE DATA FROM EACH DYNAMIC TEXT BOX AND UPDATE THAT INTO YOUR DATABASE...
For that purpose the very first thing that you need to keep in mind is the name and the ID that you are giving to the dynamically generated text boxes.
you also need to be very clear about the function "Foreach" (_Just Google it and study its parameters).
To give you an idea that using a foreach loop we can fetch all the values that are stored in a array. and the array is coming from the name and the id of the text box which were dynamically generated. viz. name="resname1[<?php echo $row['id']; ?>]"
In the above line actually I am taking an array named as resname1[] and I am storing the value of its ID in it everytime my while loop goes for a record.
Now in the update.php I am writing the code for updating the record generated.
<?php
include("../Connections/DB.php");
include("includes/functions.php");
session_start();
//print_r($_POST); ----> Use this line to study what and how the data is coming from the previous page this is very useful
foreach ( $_POST[resname] as $_id=>$_value)
{
$query=mysql_query("UPDATE list1 SET resname='$_value' WHERE id= '$_id'");
}
foreach ( $_POST[resname1] as $_id=>$_value)
{
$query=mysql_query("UPDATE list1 SET txt1='$_value' WHERE id= '$_id'");
}
foreach ( $_POST[resname2] as $_id=>$_value)
{
$query=mysql_query("UPDATE list1 SET txt2='$_value' WHERE id= '$_id'");
}
//echo "records updated successfully, you will be redirected to the main page in few seconds";
header("Location:index.php");
?>
In the above for each loop I am taking the array storing its elements in a variable named as $_value.
Say for example
resname[Array[1]=>Joe, Array[2]=>Smith]
So this way I can update my corresponding value and its record
For each of the dynamic text box generated we can update its value in the database, also PLEASE NOTE THAT I HAVE USED A ID FIELD(AUTO INCREMENT) IN DATABASE as it becomes easy to compare and update the unique record.
Now the next step what I am trying to achieve is updating the value of each text box on the real time basis that means using AJAX, if any one of you have any inputs for that then let me know...
Cheers !
Happy Coding
This Post is in reference to the time I spent in order to write just a piece of code in PHP, though it would be very easy for most of the guys but if you are still learning then its a good thing to be proud of.
Ok let me explain a little about what I was trying to do and how I did it.
Requirement: I need to list down all the names of guys who falls under a certain category and display them on another page.
I can do this by creating static text box in the landing page and fetch the value from the database OR
I can dynamically generate the text box and display all the data there.
For that first call the sql query of the condition like this.
$sql = mysql_query("SELECT * FROM `list1` WHERE BLAH BLAH");
Now use the While query in order to get all the data required
<?php
while ($row = mysql_fetch_array($sql))
{
?>
<tr>
<td align="center" valign="middle" scope="row">
<input type="text" name="resname[<?php echo $row['id']; ?>]" id="resname[<?php echo $row['id']; ?>]" value="<?php echo $row['resname'];?>"> </td>
<td align="center" valign="middle" scope="row">
<input type="text" name="resname1[<?php echo $row['id']; ?>]" id="resname1[<?php echo $row['id']; ?>]" value="<?php echo $row['txt1'];?>"></td>
<td align="center" valign="middle" scope="row">
<input type="text" name="resname2[<?php echo $row['id']; ?>]" id="resname2[<?php echo $row['id']; ?>]" value="<?php echo $row['txt2'];?>"> </td>
</tr>
<?php
}
?>
The above piece of code is dynamically generating the text Box and the values are getting displayed on every text box.
Now the major problem comes in.......HOW WOULD YOU GET THE DATA FROM EACH DYNAMIC TEXT BOX AND UPDATE THAT INTO YOUR DATABASE...
For that purpose the very first thing that you need to keep in mind is the name and the ID that you are giving to the dynamically generated text boxes.
you also need to be very clear about the function "Foreach" (_Just Google it and study its parameters).
To give you an idea that using a foreach loop we can fetch all the values that are stored in a array. and the array is coming from the name and the id of the text box which were dynamically generated. viz. name="resname1[<?php echo $row['id']; ?>]"
In the above line actually I am taking an array named as resname1[] and I am storing the value of its ID in it everytime my while loop goes for a record.
Now in the update.php I am writing the code for updating the record generated.
<?php
include("../Connections/DB.php");
include("includes/functions.php");
session_start();
//print_r($_POST); ----> Use this line to study what and how the data is coming from the previous page this is very useful
foreach ( $_POST[resname] as $_id=>$_value)
{
$query=mysql_query("UPDATE list1 SET resname='$_value' WHERE id= '$_id'");
}
foreach ( $_POST[resname1] as $_id=>$_value)
{
$query=mysql_query("UPDATE list1 SET txt1='$_value' WHERE id= '$_id'");
}
foreach ( $_POST[resname2] as $_id=>$_value)
{
$query=mysql_query("UPDATE list1 SET txt2='$_value' WHERE id= '$_id'");
}
//echo "records updated successfully, you will be redirected to the main page in few seconds";
header("Location:index.php");
?>
In the above for each loop I am taking the array storing its elements in a variable named as $_value.
Say for example
resname[Array[1]=>Joe, Array[2]=>Smith]
So this way I can update my corresponding value and its record
For each of the dynamic text box generated we can update its value in the database, also PLEASE NOTE THAT I HAVE USED A ID FIELD(AUTO INCREMENT) IN DATABASE as it becomes easy to compare and update the unique record.
Now the next step what I am trying to achieve is updating the value of each text box on the real time basis that means using AJAX, if any one of you have any inputs for that then let me know...
Cheers !
Happy Coding
Comments
Post a Comment