Solved

php loops

Posted on 2010-09-09
10
320 Views
Last Modified: 2013-12-13
I have a form called form3 with the following input boxes:
id1 status1
id2 status2
id3 status3
id4 status4

I need to be able to update records in a mysql database based on SELECTED id values with new status values selected in the status boxes
Each of the boxes is a dropdown feeding off a database of orders and refering to ID of orders and current status

What I am trying to do is to select an id value in each of the boxes and the submit the new status value to the database by using some sort of loop.
I need the php loop to look at the value of id1 and submit the new status1 value of id1 to the database, then the same for id2 / status2 etc

The problem is that I am not so familiar with php loops and would really appreciate some help here
0
Comment
Question by:doctorbill
  • 5
  • 5
10 Comments
 
LVL 42

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 33642558
Hey doctorbill.

If you want to loop through various form fields and treat them the same, it makes it easier if they're in an array. Once the form is posted, you can loop through the array and deal with the values.

//HTML

<input type="text" name="id[1]" />

<input type="text" name="status[1]" />



<input type="text" name="id[2]" />

<input type="text" name="status[2]" />



<input type="text" name="id[3]" />

<input type="text" name="status[3]" />



<input type="text" name="id[4]" />

<input type="text" name="status[4]" />





//PHP

foreach ($_POST['id'] as $key => $id)

{

	echo "The ID for Key $key is $id";

	echo "The Status for Key $key is " . $_POST['status'][$key];

}

Open in new window

0
 

Author Comment

by:doctorbill
ID: 33648608
could you give and example of how would this script be actioned by clicking an a submit button ?
0
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 33648853
The code above pretty much sums it up. You create a form with the relevant fields, and set it's action to a PHP script.

In the PHP script you loop through the form values and do whatever you need to do with them.

You can often submit the form back to itself. If you do this, check if the POST array is set before you process it.






//form.php

<?php

if ( ! empty($_POST) )

{

  //POST is not empty so it's safe to loop through your fields.

  foreach ($_POST['id'] as $key => $id)

  {

	echo "The ID for Key $key is $id";

	echo "The Status for Key $key is " . $_POST['status'][$key];

  }

}

?>



<form action="form.php" method="POST">

  <input type="text" name="id[1]" />

  <input type="text" name="status[1]" />



  <input type="text" name="id[2]" />

  <input type="text" name="status[2]" />



  <input type="text" name="id[3]" />

  <input type="text" name="status[3]" />



  <input type="text" name="id[4]" />

  <input type="text" name="status[4]" />



  <input type="submit" />

</form>

Open in new window

0
 

Author Comment

by:doctorbill
ID: 33648991
ok - I understand the form action sends the data to the form.php page.
The form.php page has the following code:
------
//form.php
<?php
if ( ! empty($_POST) )
{
  //POST is not empty so it's safe to loop through your fields.
  foreach ($_POST['id'] as $key => $id)
  {
      echo "The ID for Key $key is $id";
      echo "The Status for Key $key is " . $_POST['status'][$key];
  }
}
?>
------
How is the data sent to the database from the form.php page - I presume I need some code to send the data into the correct table
0
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 33649071
That's right,

Firstly you'll need to sanitize your fields (to prevent someone from typing something nasty), and then connect to your database and insert your records.

That's a whole different topic to PHP Loops :)



0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:doctorbill
ID: 33649244
I appreciate the scripts you have shown - they really explain things well.
Could I push you to link the above scripts to a fictitious database connection and table so that I could see how the records would be inserted into the database ?
0
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 33649578
@doctorbill

That's covering a lot of areas!!

Do you already have your database set up on a server?
What engine are you using (MS SQL, mySQL etc)?
Do you know how to perform basic database queries (Create, Retrieve, Update, Delete - CRUD)?
Have you set up and tested your connection in PHP?
Do you know how to sanitize your fields?

...The list goes on. Coding sites using PHP and mySQL is huge area.

I get the feeling that you're just starting out with PHP and Database, so would suggest that you have a read up on it, at least enough so you understand the basics of what you are trying to achieve.

Have a look at an easy-to-follow tutorial here:- http://www.phpf1.com/tutorial/php-mysql-tutorial.html.

Of course, you've also got the PHP and Database zones here on EE :)

Once you've got the basic understanding and are trying to implement what you've learned, not only will you know enough to ask the right questions to specific problems, but you'll also understand the answers that the Experts offer.

Without a basic understanding, any code I post here will simply generate more questions, all of which are off-topic - PHP Loops.

Good luck with it

Chris








0
 

Author Comment

by:doctorbill
ID: 33650136
The answer is yes to all the questions. I do a lot of php database work but I have not used loops and arrays, hence the initial question.
I just want to see how the posted information above is put into a database table. I am used to using dreamweaver and mysql insert statements, but I can't see how the posted array data links into the insert statement.
I appreciate all your efforts on this - could you just demonstrate how the information in the array is actually passed into a table ?
0
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 33651259
OK.

I don't know your table structure, so you'll have to adapt the code to meet your needs. Also not sure whether you're wanting to run UPDATE queries or INSERT queries but the principles are the same.



<?php

foreach ($_POST['id'] as $key => $value)

{

	//Clean and validate your inputs!

	$yourID = mysql_real_escape_string($value);

	$yourStatus = mysql_real_escape_string($_POST['status'][$key]);



	//Create your SQL Statement

        //Are we INSERTing

	$sqlString = "INSERT INTO yourTable (ID, Status) VALUES ('$yourID', '$yourStatus');";



        //or are we UPDATING

	$sqlString = "UPDATE yourTable SET Status = '$yourStatus' WHERE ID = '$yourID';";



	//Run your Query

	$result = mysql_query($sqlString);

	

	//If it failed, then show the reason why

	if (!$result) die('Oh oh! ' . mysql_error());

	

	//See how many rows were affected.

	$numberOfRowsAffected = mysql_affected_rows($result);

}

Open in new window

0
 

Author Closing Comment

by:doctorbill
ID: 33665210
Thanks so much for your patience and extremely clear descriptions and documentation - the code works fine
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now