Hi :)

I got a problem with 2 foreach. When i execute it the result repeats X times.

This is my code:

$tab1=array('name','firstname','age');
$exceptions=array('hide','submitForm');

$test='';
foreach($tab1 as $val){
    $test.=$val.'=';
    foreach($_POST as $key=>$value){
        if(!in_array($key,$exceptions)){
            $test.=$value.',';  
        }   
    }
}
echo $test;

This is the result obtained:
name=Smith,John,30,firstname=Smith,John,30,age=Smith,John,30,

I would like to obtain this:
nom_test=Smith,prenom_test=John,age_test=30

Thanks for your help :)

Recommended Answers

All 17 Replies

Member Avatar for LastMitch

@mano7859

I got a problem with 2 foreach. When i execute it the result repeats X times.

I'm a bit confused when said 2 foreach?

So you want to get this results:

nom_test=Smith,prenom_test=John,age_test=30

-

$tab1=array('nom_test=','prenom_test=','age_test=');

$exceptions=array('hide','submitForm');

$test='';

foreach($tab1 as $val){

   $test.=$val.',';

    foreach($_POST as $key=>$value){

       if(!in_array($key,$exceptions)){

            $test.=$value.'';  
        }   
    }
}

echo $test;

When you echo out it should look like:

nom_test=Smith,prenom_test=John,age_test=30

Is this what you are asking?

commented: To Rectify what some retard did to LastMitch +0

I did a mistake when i wrote my problem.

I want to get:

name=smith,firstname=john,age=30

When I execute my code, i have:

name=,smithjohn30first=,smithjohn30age=,smithjohn30name=,smithjohn30first=,smithjohn30age=,smithjohn30

Member Avatar for LastMitch

@mano7859

You want to get this:

name=smith,firstname=john,age=30

Try now:

<?php

$tab1=array('name','first','age');
$exceptions=array('hide','submitForm');
$test='';
foreach($tab1 as $val){
    $test.=$val.'='.',';
    foreach($_POST as $key=>$value){
        if(!in_array($value,$exceptions)){
          $test.=$value.'';  
        }   
    }
}
echo $test;

?>

It should echo out this:

name=smith,firstname=john,age=30

You have to understand I don't have a form to work on, I'm just working on the code that you provided.

commented: To Rectify what some retard did to LastMitch +0

Thanks for your help :)

After trying your code, i have this echo out:

name=,smithjohntest30first=,smithjohntest30age=,smithjohntest30

(but not: name=smith,firstname=john,age=30)

Member Avatar for LastMitch

@mano7859

<?php

$tab1=array('name','first','age');
$exceptions=array('hide','submitForm');
$test='';
foreach($tab1 as $val){
    $test .='='.$val.',';
    foreach($_POST as $key=>$value){
        if(!in_array($value,$exceptions)){
          $test.=$value.''; 
        }   
    }
}
echo $test;

?>

Try it now.

I get this echo out :(

=name,smithjohn30=first,smithjohn30=age,smithjohn30

Member Avatar for LastMitch

@mano7859

I don't have a form so I'm thinking backwards base on your code.

What do you get with this:

<?php
$tab1=array('name','first','age');
$exceptions=array('hide','submitForm');
$test='';
foreach($tab1 as $val){
    $test .=$val.'=';
    foreach($_POST as $key=>$value){
        if(!in_array($value,$exceptions)){
          $test.=$value.''; 
        }   
    }
}
echo $test;
?>

On here: $test .=$val.'=';

What did you get?

Something like this:

name=first=age=

without the comma','

On here: $test .=$val.'=';

What did you get?

I got : name=name=smithjohn30first=name=smithjohn30first=smithjohn30age=

In your last code, i've modified this:

 if(!in_array($value,$exceptions)){

By:

if(!in_array($key,$exceptions)){

But even without modification, I get this echo out with your last code: name=smithjohn30first=smithjohn30age=smithjohn30

My form is very simple:

<form action="#" method="post" enctype="multipart/form-data">

<label>Your name:</label><input type="text" name="name" /><br/>
<label>Your firstname:</label><input type="text" name="firstname" /><br/>
<input type="hidden" name="hide" value="test">
<label>Your age:</label><input type="text" name="age" /></br>

<input type="submit" name="submitForm" value="Submit">

</form>
Member Avatar for LastMitch

@mano7859

Do you have a DB connected with this code? I assume if you do have one then the example will make more sense because you SELECT the name, firstname, age from a table

<form action="#" method="post" enctype="multipart/form-data">

<label>Your name:</label><input type="text" name="name" /><br/>
<label>Your firstname:</label><input type="text" name="firstname" /><br/>
<input type="hidden" name="hide" value="test">
<label>Your age:</label><input type="text" name="age" /></br>

<input type="submit" name="submitForm" value="Submit">
</form>   

-

<?php
$tab1=array('name','firstname','age');

$exceptions=array('hide','submitForm');

$test='';

foreach($tab1 as $val){

    $test.=$val.'=';

    foreach($_POST as $key=>$value){

    @$select.=" `".mysql_real_escape_string($key)."` = '".mysql_real_escape_string($value)."',";

}
$select = rtrim($select,',');

$select = "UPDATE table_name SET".$select." WHERE id=".$_POST['id'];

mysql_query($select) or die(mysql_error());;

        if(!in_array($key,$exceptions)){

            $test.=$value.'';  
        }   
    }

echo $test;  
?>

Hi LastMitch, sorry to answer u late. I hope u're fine ;)

Your function is great but it doesn't work in my case... :(

I will explain you all in details:

1 / I have my simple form:

    <form action="#" method="post" enctype="multipart/form-data">
    <label>Your name:</label><input type="text" name="name" /><br/>
    <label>Your firstname:</label><input type="text" name="firstname" /><br/>
    <input type="hidden" name="hide" value="test">
    <label>Your age:</label><input type="text" name="age" /></br>
    <input type="submit" name="submitForm" value="Submit">
    </form> 

2/ I want to extract all fields that I don't want (here: hide and submitForm). To do that, I've created an array:

$exceptions=array('hide','submitForm'); 

3/ I want to update mysql fields. So i've mentionned them inside an array:

$tab1=array('name_User','first_User', 'age_User');

4/ The idea with this function it's to update my fields, with your is fine but it doesn't take the mysqlFields. When I execute it: i have:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATEUtilisateurs SET name = 'smith', firstname = 'john', age = '30'' at line 1

And me I want to have name_User='Smith', firstname_user='John', age_User='30'

Thanks for your help ;)

Member Avatar for LastMitch

@mano7859

Do you have a DB? The only way for this to work is that you have DB select

$tab1=array('name','firstname','age');

Then you can select a word or number from each category:

name=smith,firstname=john,age=30

I want to extract all fields that I don't want (here: hide and submitForm). To do that, I've created an array:

I know that you want to extract a word from each:

name=smithjohntest30first=smithjohntest30age=smithjohntest30

to get this:

name=smith,firstname=john,age=30

The idea with this function it's to update my fields, with your is fine but it doesn't take the mysqlFields

This is how you select:

SELECT FROM WHERE id NOT IN (SELECT id FROM WHERE = 'smith' ) ORDER BY RAND() LIMIT 1

Do you have a DB? The only way for this to work is that you have DB select

Yes I have a Db select like that:

$reqSteName=mysql_query("SELECT name_User,first_User,age_User FROM Users WHERE id_users='1'") or die(mysql_error));

Html fields are automaticaly updating with these datas.

After, I've modified and submit I want to update datas.

I've already create a php class to insert and I wanted to do the same with the update.

Example of my insert class:

public function insertData($chpSql,$except){
        $postForm='';
        $listSql='';

        if($this->type=='$_POST'){
            foreach($_POST as $key=>$val){
                if(!in_array($key,$except)){
                    $postForm .= "'".$val."',";
                }
            }
            foreach($chpSql as $val){
                $listSql.= $val.", ";
            }
            $reqInsert=mysql_query("INSERT INTO ".$this->table." (".substr($listSql,0,-2).") VALUE (".substr($postForm,0,-1).")") or die(mysql_error());
        }
    }

So, for you, it's not necessary to create an array with mysql fiels - $tab1=array('name_User' ...); - That's it?

I'm sorry but can you explain me with an example please because I don't find issue :s

Member Avatar for LastMitch

@mano7859

$reqSteName=mysql_query("SELECT name_User,first_User,age_User FROM Users WHERE id_users='1'") or die(mysql_error));

You do have a DB that's good but there's an error in your SELECT what error do you get?

public function insertData()

You are using object oriented programming (OOP).

Are you using any PHP Frameworks?

I haven't learn OOP yet, I still working and learn Smarty on my own.

At first I thought it was a foreach () issue but now after see the whole code it's more than that.

Was the array part of the problem above or you just create the array?

$tab1=array('nom_test=','prenom_test=','age_test=');
$exceptions=array('hide','submitForm');

Let's organized this:

1) This is the form:

<form action="#" method="post" enctype="multipart/form-data">
<label>Your name:</label><input type="text" name="name" /><br/>
<label>Your firstname:</label><input type="text" name="firstname" /><br/>
<input type="hidden" name="hide" value="test">
<label>Your age:</label><input type="text" name="age" /></br>
<input type="submit" name="submitForm" value="Submit">
</form> 

2) This the Insert:

public function insertData($chpSql,$except){
$postForm='';
$listSql='';
if($this->type=='$_POST'){
foreach($_POST as $key=>$val){
if(!in_array($key,$except)){
$postForm .= "'".$val."',";
}
}
foreach($chpSql as $val){
$listSql.= $val.", ";
}
$reqInsert=mysql_query("INSERT INTO ".$this->table." (".substr($listSql,0,-2).") VALUE (".substr($postForm,0,-1).")") or die(mysql_error());
}
}

3) This is the Update:

$tab1=array('name','first','age');
$exceptions=array('hide','submitForm');
$test='';
foreach($tab1 as $val){
$test .=$val.'=';
foreach($_POST as $key=>$value){
if(!in_array($value,$exceptions)){
$test.=$value.''; 
}   
}
}
echo $test;

I've already create a php class to insert and I wanted to do the same with the update.

Since you used public function insertData() for Number 2.

You need to used public function update() for Number 3.

in order for it to work!

You do have a DB that's good but there's an error in your SELECT what error do you get?

I forgot parenthesis. I was distracted ;)

$reqSteName=mysql_query("SELECT name_User,first_User,age_User FROM Users WHERE id_users='1'") or die(mysql_error());

Are you using any PHP Frameworks?

Yep, I use one. I've create mine and it works fine to separate html, php...
For me, smarty is too much heavy for my using but that's my opinion ;) I know you could say to me: "Shame on you" ;)

Was the array part of the problem above or you just create the array?

I'm creating my array depending on my form. For example, I can have a hide field and sometimes no.
So, in my example, I've created:

    $tab1=array('nom_test=','prenom_test=','age_test=');
    $exceptions=array('hide','submitForm');

You need to used public function update() for Number 3.

You're right of course ;)
I've created it follow your advises

public function updateData($chpSql,$except,$idSql,$id){
            $listSql='';
            foreach($chpSql as $val){
            $listSql.=$val.'=';
                foreach($_POST as $key=>$value){
                    if(!in_array($key,$except)){
                        @$select.=" `".mysql_real_escape_string($key)."` = '".$value."',";
                    }
                }
            $select = rtrim($select,',');
            $select = mysql_query("UPDATE ".$this->table." SET".$select." WHERE ".$idSql."=".$id) or die(mysql_error());

            }

    }

but I got this message:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE maTable SET `nom` = 'smith', `prenom` = 'john', `age` = '30' WHERE id_User' at line 1

Like you see I just have the html fields but not my sql fields from $tab1 :(

Member Avatar for LastMitch

@mano7859

For me, smarty is too much heavy for my using but that's my opinion ;) I know you could say to me: "Shame on you" ;)

I'm not gonna say that to anyone, it will be rude.

MySQL server version for the right syntax

'UPDATE maTable SETnom= 'smith',prenom= 'john',age= '30' WHERE id_User'

It's mean that you wrote the Update statement wrong you need to fixed it.

Here is an link just to show you to corrected it:

http://www.tizag.com/mysqlTutorial/mysqlupdate.php

Hi LastMitch,

Hope u're fine ;)

I have good news. I found my solution and it works well (after throwing my brain on a wall) :)

Now I have a nice mysql class to insert, update and delete.

Next step will be with a while to show value from mysql :/

Thanks for your help :))

Member Avatar for LastMitch

@mano7859

I have good news. I found my solution and it works well (after throwing my brain on a wall) :)

I'm glad you my your solution!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.