Jump to content

[SOLVED] loop help plz


gevo12321

Recommended Posts

ok i have this code but it doesnt work how can i change it to make it work?

else if (isset($edit))
{
  foreach ($_POST['button'] as $namekey => $namevalue) 
  {
  $name[$namekey] = $namevalue;
  }
  foreach ($_POST['buttonsrc'] as $sourcekey => $sourcevalue)
  {
  $source[$sourcekey] = $sourcevalue;
  }
  foreach ($_POST['id'] as $idkey => $idvalue)
  {
  $id[$idkey] = $idvalue;
  }
mysql_query("UPDATE layout SET 'name'='".$name[$namekey]."', 'source'='".$source[$sourcekey]."'
WHERE id='".$id[$idkey]."'");
}

Link to comment
Share on other sites

ok i have this code but it doesnt work how can i change it to make it work?

else if (isset($edit))
{
  foreach ($_POST['button'] as $namekey => $namevalue) 
  {
  $name[$namekey] = $namevalue;
  }
  foreach ($_POST['buttonsrc'] as $sourcekey => $sourcevalue)
  {
  $source[$sourcekey] = $sourcevalue;
  }
  foreach ($_POST['id'] as $idkey => $idvalue)
  {
  $id[$idkey] = $idvalue;
  }
mysql_query("UPDATE layout SET 'name'='".$name[$namekey]."', 'source'='".$source[$sourcekey]."'
WHERE id='".$id[$idkey]."'");
}

 

hmm your code is weird pass the variable into the array to be use while others get array content and pass to variables to be used hmm thats totally different 

Link to comment
Share on other sites

1st explain this

foreach ($_POST['button'] as $namekey => $namevalue)

  {

  $name[$namekey] = $namevalue;

  }

  foreach ($_POST['buttonsrc'] as $sourcekey => $sourcevalue)

  {

  $source[$sourcekey] = $sourcevalue;

  }

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

  {

  $id[$idkey] = $idvalue;

  }

Link to comment
Share on other sites

mysql_query("UPDATE layout SET 'name'='".$name[$namekey]."', 'source'='".$source[$sourcekey]."'
WHERE id='".$id[$idkey]."'");
}

 

You should use only name or `name` instead 'name'.

 

another thing this query will work only for last values of the arrays ($name,$source,$id)

 

--

Tapos Pal

Link to comment
Share on other sites

well the arrays are defined as:

  foreach ($_POST['button'] as $namekey => $namevalue) 
  {
  $name[$namekey] = $namevalue;
  }
  foreach ($_POST['buttonsrc'] as $sourcekey => $sourcevalue)
  {
  $source[$sourcekey] = $sourcevalue;
  }
  foreach ($_POST['id'] as $idkey => $idvalue)
  {
  $id[$idkey] = $idvalue;
  }

i dont know if that was what u meant by value

 

as for the length, the number of elements in each array is variable

i dont know if thats what u meant by length

 

thx

Link to comment
Share on other sites

You can try this:

$i = 0;
$info = array();
foreach ($_POST['button'] as $namekey => $namevalue) 
  {
   $info[$i++]['name'] = $namevalue;
  
  }
  $i=0;
  foreach ($_POST['buttonsrc'] as $sourcekey => $sourcevalue)
  {
  $info[$i++]['source'] = $sourcevalue;
  
  }
  $i=0;
  foreach ($_POST['id'] as $idkey => $idvalue)
  {
   $info[$i++]['id'] = $idvalue;
  
  }
//queries
for($i =0;$i<count($info);$i++)
{
mysql_query("UPDATE layout SET name='".$info[$i]['name']."', source='".$info[$i]['source']."'
WHERE id='".$info[$i]['id']."'");
}

--

Tapos Pal

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.