Solved

php loops

Posted on 2010-09-09
10
336 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 43

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 43

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
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 

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 43

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
 

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 43

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 43

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

632 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