Jump to content

Posting an array problems


sidesteal

Recommended Posts

I have a db table with 1500 street names inside - id, tfare, ptown, pcode, area.

 

The 'area' column is specific to the company i work for and each street belongs to an 'area' number defined by the company.

 

Currently each area is '0' - unassigned. The company areas are numbered 1 - 18. I need to list each street (LIMIT 10 at a time), and have an input box for the area to be edited and saved.

 

The following code works perfectly well when dealing with 1 record at a time (LIMIT 0, 1), but breaks when i change the LIMIT to 10.

 

I'm trying to show the dinosaur management that we need to upgrade our paper, yes, paper system to computers. I'm a transport manager, but in my spare time since 2004 I'm fluent in HTML and CSS, but only in the last few months really decided to give PHP and MySQL a *real* go.

 

I'm 'guessing' I need to look into arrays for this to work? I've tried searching about but I dont even know how to articulate what i need to search for, so I have to publicly ask for help via this long winded post, sorry :)

 

Here is my code:

 

[pre]

 

<?php

 

include($_SERVER['DOCUMENT_ROOT'].'/!SYSTEM/bootstrap.php');

 

if(isset($_POST['savearea']))

{

    $id = $_POST['hideID'];

    $area = $_POST['area'];

   

    $sql = "UPDATE bmth_streetindex SET area='$area' WHERE id='$id'";

   

    if ($query = mysql_query($sql)or die(mysql_error()))

        {

        print "saved ???";

        }

}

 

    // grab data from bmth street index where the area is = 0 (which means its area is un-assigned)

    $sql = "SELECT * FROM bmth_streetindex WHERE area='0' LIMIT 0, 10";

    $rs = mysql_query($sql) or die(mysql_error());

 

 

print $HTML::form('action="" name="inputAreaForm" method="POST"')."\n\n";

   

    // 'while' thru and print them 1 at a time

    while($row = mysql_fetch_array($rs))

    {

        $id = $row['id'];

        $tfare = $row['tfare'];

        $ptown = $row['ptown'];

        $pcode = $row['pcode'];

        $area = $row['area'];

        $urlQ = $tfare. ", " .$ptown. ', ' .$pcode;

       

        print "<div>\n";

       

            print "\n<input type=\"hidden\" name=\"hideID\" value=\"$id\">";

           

            print "\n[$id] $tfare, $ptown, $pcode    ";

            print "\nArea <input type=\"text\" name=\"area\" value=\"$area\" />";

            print "\n<input type=\"submit\" name=\"savearea\" value=\"Save Area\">";

           

            print "\n<a href=\"http://www.google.com/maps?q=$urlQ&hl=en&ie=UTF8&z=17\" target=new>SHOW ON MAP</a>";

           

        print "\n<>\n<hr>\n\n";

    }

 

print $HTML::endForm();

 

 

?>

 

[/pre]

 

 

Thank you for taking the tie to read this, I hope someone can point me in the right direction for what i need to look into making this work :)

Link to comment
Share on other sites

If you have multiple inputs with names like "area" then PHP will create one $_POST["area"] and overwrite it for every one in the form.

However you can name them with a special array syntax and $_POST["area"] will be an array rather than a single value.


You can extend this to multiple arbitrary array keys (so long as there's at most one "[]" and it probably has to be at the end).


With that second syntax, "0" would be the ID number of the row, otherwise you'd have to put the ID in the form in its own special hidden field.

foreach ($_POST["area"] as $id => $area) {
    // update #$id, set area = $area
}

Link to comment
Share on other sites

Hi, I do have the id in a hidden field and tried using your code but but it didnt work, which is probably down to the lack of understanding i have with your answer. I know it's possible as I could do this in phpMyadmin by ticking the boxes of every record and editing them in bulk - but as other non computer savvy people will be inputting the areas for me, i'd rather not let them loose in the db.

 

Hopefully you or someone else can expand on this further. Thank you so far :)

Link to comment
Share on other sites

Maybe this might get you going.

<?php
include($_SERVER['DOCUMENT_ROOT'].'/!SYSTEM/bootstrap.php');

if(isset($_POST['savearea'])){
if (isset($_POST['hideID']) && !empty($_POST['area'])){
	foreach($_POST['hideID'] as $k => $id){ 
	$area=mysql_real_escape_string($_POST['area'][$k]);
	mysql_query("UPDATE bmth_streetindex SET area='$area' WHERE id='$id'");
	}
}
}
// grab data from bmth street index where the area is = 0 (which means its area is un-assigned)
    $sql = "SELECT * FROM bmth_streetindex WHERE area='0' LIMIT 0, 10";
    $rs = mysql_query($sql) or die(mysql_error());
$form='<form action="" name="inputAreaForm" method="post">'."\n\n";    
    // 'while' thru and print them 1 at a time
    while($row = mysql_fetch_array($rs))
    {
        $id = $row['id'];
        $tfare = $row['tfare'];
        $ptown = $row['ptown'];
        $pcode = $row['pcode'];
        $area = $row['area'];
        $urlQ = $tfare. ", " .$ptown. ', ' .$pcode;
        
$form.="<div>\n";
$form.="\n<input type=\"hidden\" name=\"hideID[]\" value=\"$id\">";
$form.="\n[$id] $tfare, $ptown, $pcode    ";
$form.="\nArea <input type=\"text\" name=\"area[]\" value=\"$area\" />";
$form.="\n<a href=\"http://www.google.com/maps?q=$urlQ&hl=en&ie=UTF8&z=17\" target=new>SHOW ON MAP</a>";
$form.="</div>\n";
$form.="\n<>\n<hr>\n\n";
    } 
$form.="\n<input type=\"submit\" name=\"savearea\" value=\"Save Area\">";
$form.="</form>\n";
echo "$form";
?>

Link to comment
Share on other sites

Brilliant, that works perfectly ! Exactly what i'm looking for :)

 

I have to work all over the weekend there now, but i'm taking the laptop to work and will examine your amendments in great detail tonight to fully grasp your changes.

 

Many, many thanks, I wont leave it on this, I will post back with my grasp of what you did.

 

Big thank you :)

Link to comment
Share on other sites

Finally had enough time to sit down and look at the changes you made [Drummin]

 

I think I understand what you have done, so I will explain it the way i see it.

 

You added [] to the input names. This is PHP specific and as a result a POST array is formed (for each input hideID and area)

 

If the POST hideID is set and POST area is not empty, then for each element in the POST hideID array you set the key to variable $k and it's value to $id and set $area to a sanitized POST area with a matching key - its this part that keeps them paired up.  The SQL is then updated with the correct values in each 'loop'.

 

Well, thats my plain english understanding of it. It took me a while to grasp it, and var_dump helped to visualise what data is being stored.  That part help greatly to understand the second use of the $k variable.

 

Big thanks Drummin :)

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.