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
https://forums.phpfreaks.com/topic/59346-solved-loop-help-plz/
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
https://forums.phpfreaks.com/topic/59346-solved-loop-help-plz/#findComment-294834
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
https://forums.phpfreaks.com/topic/59346-solved-loop-help-plz/#findComment-294884
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
https://forums.phpfreaks.com/topic/59346-solved-loop-help-plz/#findComment-295060
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
https://forums.phpfreaks.com/topic/59346-solved-loop-help-plz/#findComment-295069
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
https://forums.phpfreaks.com/topic/59346-solved-loop-help-plz/#findComment-295077
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.