help with two dimensional array

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
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

help with two dimensional array

Post by ddragas »

how to post values from textfields to recive them and isert them into two dimensional array


Untill now I have this, bit it is not working.

Code: Select all

<?

if(isset($_POST['Submit']))
	{
		$a=0;
		$data=array();
		foreach ($_POST as $fieldName => $value)
		 {
				if($fieldName == 'broj')
					{
						$broj = $value;
					}

				   if ($fieldName != 'broj' && $fieldName != 'Submit') 
				   {
					   if($fieldName == "ime$a")
							{
							   $value_ime = $value;
							}
						if($fieldName == "prezime$a")	
							{
								$value_prezime = $value;
							}
						
						
						$data[]=array($value_ime, $value_prezime);
						
					$a=$a+1;
					}
		
		}
	echo "Ukupno " . $broj . " polja.";
	print_r($data);
	}

		else
	{

	$m = 3;
		
		//$ime = array();
		//$prezime = array();
		echo "<br>";
		echo "<form enctype=\"multipart/form-data\" action=\"\" method=\"POST\">";
		echo "<table width=\"80%\"  border=\"0\" align=\"center\" bordercolor=\"#000000\">";
		echo "<input type=\"hidden\" name=\"broj\" value=\"$m\">";
		for ($i=0; $i < $m; $i++)
      	{
	  		$broj = $i+1;
      		echo"<td width=\"40%\" align=\"right\">" . "Slika " . $broj . ": " ."</td><td>"; 
			echo "<input type=text name=ime" . $i . ">";
			echo "</td>";
			echo "<td>";
			echo "<input type=text name=prezime" . $i . ">";
			echo "</td>";
		    echo "</tr>";
      	}
		echo "<td></td></tr>";
        echo "<td></td><td><input type=\"submit\" name=\"Submit\" value=\"Pridruži / izmjeni slike\" onclick=\"value='Molim pri&#269;ekajte...'\" class=\"txt\">";
        echo "<input name=\"Reset\" type=\"reset\" id=\"Reset\" value=\"Poništi\" class=\"txt\"></td></tr>";
	   
		echo "</table>";
		echo "</form>";
	}
?>
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

Hope U understand what I need help with?


there can be X number of text fields with name and surname
posts from form text fields like:

NAME SURNAME

Mark Johnson
Piter Young
Andy Hofner

etc....


I need to insert values from text fields into db.

Insert into users (name, surname) VALUES ('$name', '$surname')

What is easyest way to do this.

I tought first to put values from text fields into two dimensional array, retrive data from array and put them into db.

If I'm doing something wrong, please point me in right way
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

How is it not working? What does $data get?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

Here is resoult of array

Code: Select all

Ukupno 3 polja.Array
(
    [0] => Array
        (
            [0] => Petar
            [1] => 
        )

    [1] => Array
        (
            [0] => Petar
            [1] => 
        )

    [2] => Array
        (
            [0] => Petar
            [1] => 
        )

    [3] => Array
        (
            [0] => Petar
            [1] => 
        )

    [4] => Array
        (
            [0] => Petar
            [1] => 
        )

    [5] => Array
        (
            [0] => Petar
            [1] => 
        )

)
Post Reply