如何获取foreach循环以跳过具有空白字段的数组中的记录?

时间:2011-05-14 13:12:11

标签: php arrays foreach

我有这个foreach循环将此数组插入数据库。我希望它跳过字段tr_name中没有任何内容的记录。最好的方法是什么?

 foreach($_POST['tr_name'] as $tr_name) {
         $sql3 = "INSERT INTO trophies (game_name, tr_name, tr_description, tr_color, tr_ach_value) VALUES ('".$_POST['game_name']."', '".$tr_name['tr_name']."', '".$tr_name['tr_desc']."', '".$tr_name['tr_color']."', '".$tr_name['tr_ach_value']."')";
        mysql_query($sql3);

正确插入了字段game_name的数据。其余每个字段中只填充了tr_name字段中输入内容的第一个字符。为什么会这样?

以下是表格:

<form action="http://www.yeahthatrocks.com/update.php" method="post">
Game Name:  <input name="game_name" type="text" size="25" maxlength="255" /><br></br>
Release Date:  <input name="release_date" type="text" size="25" /><p></p>

<p>Console:
  <select name="game_console">
    <option value="PS3">PS3</option>
    <option value="Xbox 360">Xbox 360</option>
    <option value="Both">Both</option>
  </select>

  Game Category:  
  <select name="game_category">
    <option value="Retail">Retail</option>
    <option value="PSN">PSN</option>
    <option value="Arcade">Arcade</option>
    <option value="Arcade">DLC</option>
  </select>

  Game Type:  
  <select name="game_type">
    <option value="Action">Action</option>
    <option value="Action RPG">Action RPG</option>
    <option value="Adventure">Adventure</option>
    <option value="Board">Board</option>
    <option value="Card">Card</option>
    <option value="Casino">Casino</option>
    <option value="Educational">Educational</option>
    <option value="Fighting">Fighting</option>
    <option value="Flight">Flight</option>
    <option value="Game Show">Game Show</option>
    <option value="Hunting">Hunting</option>
    <option value="Music">Music</option>
    <option value="Other">Other</option>
    <option value="Pinball">Pinball</option>
    <option value="Platformer">Platformer</option>
    <option value="Puzzle">Puzzle</option>
    <option value="Racing">Racing</option>
    <option value="RPG">RPG</option>
    <option value="Shooter">Shooter</option>
    <option value="Sports">Sports</option>
    <option value="Strategy">Strategy</option>
    <option value="Virtual Pet">Virtual Pet</option>
  </select>



 Trophy Totals:</p> 

 Bronze:  <input name="bronze_ttl" type="text" size="3" maxlength="3" />
 <br/>
Silver:  <input name="Silver Total" type="text" size="3" maxlength="3" /><br/>
Gold:  <input name="Gold Total" type="text" size="3" maxlength="3" /><br/>
Platinum:  <input name="Platinum Total" type="text" size="3" maxlength="3" /><br/>
Hidden:  <input name="Hidden Total" type="text" size="3" maxlength="3" /><br/>
Xbox Pts.:  <input name="Xbox Pts Total" type="text" size="5" maxlength="5" /><br/>



</p>
<p>&nbsp;</p>
<p>Trophies:</p>
Trophy Name:    <input name="tr_name[0]" type="text" size="50" maxlength="255" /><br/>
Descripton:     <input name="tr_desc[0]" type="text" size="50" maxlength="255" /><br/>
Trophy Color:   <select name="tr_color[0]">
  <option value="Bronze">Bronze</option>
  <option value="Silver">Silver</option>
  <option value="Gold">Gold</option>
  <option value="Platinum">Platinum</option>
  <option value="Hidden">Hidden</option>
</select>
Points: <input name="tr_ach_value[0]" type="text" size="4" maxlength="4" /><p></p>

Trophy Name:    <input name="tr_name[1]" type="text" size="50" maxlength="255" /><br/>
Descripton:     <input name="tr_desc[1]" type="text" size="50" maxlength="255" /><br/>
Trophy Color:   <select name="tr_color[1]">
  <option value="Bronze">Bronze</option>
  <option value="Silver">Silver</option>
  <option value="Gold">Gold</option>
  <option value="Platinum">Platinum</option>
  <option value="Hidden">Hidden</option>
</select>
Points: <input name="tr_ach_value[1]" type="text" size="4" maxlength="4" /><p></p>

Trophy Name:    <input name="tr_name[2]" type="text" size="50" maxlength="255" /><br/>
Descripton:     <input name="tr_desc[2]" type="text" size="50" maxlength="255" /><br/>
Trophy Color:   <select name="tr_color[2]">
  <option value="Bronze">Bronze</option>
  <option value="Silver">Silver</option>
  <option value="Gold">Gold</option>
  <option value="Platinum">Platinum</option>
  <option value="Hidden">Hidden</option>
</select>
Points: <input name="tr_ach_value[2]" type="text" size="4" maxlength="4" /><p></p>

Trophy Name:    <input name="tr_name[3]" type="text" size="50" maxlength="255" /><br/>
Descripton:     <input name="tr_desc[3]" type="text" size="50" maxlength="255" /><br/>
Trophy Color:   <select name="tr_color[3]">
  <option value="Bronze">Bronze</option>
  <option value="Silver">Silver</option>
  <option value="Gold">Gold</option>
  <option value="Platinum">Platinum</option>
  <option value="Hidden">Hidden</option>
</select>
Points: <input name="tr_ach_value[3]" type="text" size="4" maxlength="4" /><p></p>

Trophy Name:    <input name="tr_name[4]" type="text" size="50" maxlength="255" /><br/>
Descripton:     <input name="tr_desc[4]" type="text" size="50" maxlength="255" /><br/>
Trophy Color:   <select name="tr_color[4]">
  <option value="Bronze">Bronze</option>
  <option value="Silver">Silver</option>
  <option value="Gold">Gold</option>
  <option value="Platinum">Platinum</option>
  <option value="Hidden">Hidden</option>
</select>
Points: <input name="tr_ach_value[4]" type="text" size="4" maxlength="4" /><p></p>

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

4 个答案:

答案 0 :(得分:3)

foreach(array_filter($_POST['tr_name']) as $tr_name) {
…

答案 1 :(得分:1)

除非tr_name为多个值(并且HTML看起来像),否则这是正确的方法。如果它只有一个值(),那就错了。

让我们坚持认为你有多个输入字段。那么这就是你要做的全部:

foreach($_POST['tr_name'] as $tr_name) {
  if($tr_name != "") {
    // insert query
  }
}

答案 2 :(得分:1)

这是你想要的吗?

foreach($_POST['tr_name'] as $tr_name) {
    if (empty($tr_name)) {
        continue;
    }

    // insert query
}

答案 3 :(得分:0)

您可以使用continue;来控制您的foreach循环:

foreach($_POST['tr_name'] as $tr_name) {

  if(empty($tr_name)) { continue; }

  $sql3 = "INSERT INTO trophies (game_name, tr_name, tr_description, tr_color, tr_ach_value) VALUES ('".$_POST['game_name']."', '".$tr_name['tr_name']."', '".$tr_name['tr_desc']."', '".$tr_name['tr_color']."', '".$tr_name['tr_ach_value']."')";
  mysql_query($sql3);
}

修改

您的表单将生成4个独立数组,每个数组包含4个项目,如名称,描述,颜色等。它将如下所示:

tr_name[0]
tr_desc[0]

tr_name[1]
tr_name[2]

...

你可能想要的是一个包含4个内部数组的数组,其中有4个项目,如:

tr[0][name]
tr[0][desc] 

tr[1][name]
tr[1][desc]
...

新表格:

<form action="http://www.yeahthatrocks.com/update.php" method="post">
Game Name:  <input name="game_name" type="text" size="25" maxlength="255" /><br></br>
Release Date:  <input name="release_date" type="text" size="25" /><p></p>

<p>Console:
  <select name="game_console">
    <option value="PS3">PS3</option>
    <option value="Xbox 360">Xbox 360</option>
    <option value="Both">Both</option>
  </select>

  Game Category:  
  <select name="game_category">
    <option value="Retail">Retail</option>
    <option value="PSN">PSN</option>
    <option value="Arcade">Arcade</option>
    <option value="Arcade">DLC</option>
  </select>

  Game Type:  
  <select name="game_type">
    <option value="Action">Action</option>
    <option value="Action RPG">Action RPG</option>
    <option value="Adventure">Adventure</option>
    <option value="Board">Board</option>
    <option value="Card">Card</option>
    <option value="Casino">Casino</option>
    <option value="Educational">Educational</option>
    <option value="Fighting">Fighting</option>
    <option value="Flight">Flight</option>
    <option value="Game Show">Game Show</option>
    <option value="Hunting">Hunting</option>
    <option value="Music">Music</option>
    <option value="Other">Other</option>
    <option value="Pinball">Pinball</option>
    <option value="Platformer">Platformer</option>
    <option value="Puzzle">Puzzle</option>
    <option value="Racing">Racing</option>
    <option value="RPG">RPG</option>
    <option value="Shooter">Shooter</option>
    <option value="Sports">Sports</option>
    <option value="Strategy">Strategy</option>
    <option value="Virtual Pet">Virtual Pet</option>
  </select>



 Trophy Totals:</p> 

 Bronze:  <input name="bronze_ttl" type="text" size="3" maxlength="3" />
 <br/>
Silver:  <input name="Silver Total" type="text" size="3" maxlength="3" /><br/>
Gold:  <input name="Gold Total" type="text" size="3" maxlength="3" /><br/>
Platinum:  <input name="Platinum Total" type="text" size="3" maxlength="3" /><br/>
Hidden:  <input name="Hidden Total" type="text" size="3" maxlength="3" /><br/>
Xbox Pts.:  <input name="Xbox Pts Total" type="text" size="5" maxlength="5" /><br/>



</p>
<p>&nbsp;</p>
<p>Trophies:</p>
Trophy Name:    <input name="tr[0][name]" type="text" size="50" maxlength="255" /><br/>
Descripton:     <input name="tr[0][desc]" type="text" size="50" maxlength="255" /><br/>
Trophy Color:   <select name="tr[0][color]">
  <option value="Bronze">Bronze</option>
  <option value="Silver">Silver</option>
  <option value="Gold">Gold</option>
  <option value="Platinum">Platinum</option>
  <option value="Hidden">Hidden</option>
</select>
Points: <input name="tr[0][ach_value]" type="text" size="4" maxlength="4" /><p></p>

Trophy Name:    <input name="tr[1][name]" type="text" size="50" maxlength="255" /><br/>
Descripton:     <input name="tr[1][desc]" type="text" size="50" maxlength="255" /><br/>
Trophy Color:   <select name="tr[1][color]">
  <option value="Bronze">Bronze</option>
  <option value="Silver">Silver</option>
  <option value="Gold">Gold</option>
  <option value="Platinum">Platinum</option>
  <option value="Hidden">Hidden</option>
</select>
Points: <input name="tr[1][ach_value]" type="text" size="4" maxlength="4" /><p></p>


Trophy Name:    <input name="tr[2][name]" type="text" size="50" maxlength="255" /><br/>
Descripton:     <input name="tr[2][desc]" type="text" size="50" maxlength="255" /><br/>
Trophy Color:   <select name="tr[2][color]">
  <option value="Bronze">Bronze</option>
  <option value="Silver">Silver</option>
  <option value="Gold">Gold</option>
  <option value="Platinum">Platinum</option>
  <option value="Hidden">Hidden</option>
</select>
Points: <input name="tr[2][ach_value]" type="text" size="4" maxlength="4" /><p></p>

Trophy Name:    <input name="tr[3][name]" type="text" size="50" maxlength="255" /><br/>
Descripton:     <input name="tr[3][desc]" type="text" size="50" maxlength="255" /><br/>
Trophy Color:   <select name="tr[3][color]">
  <option value="Bronze">Bronze</option>
  <option value="Silver">Silver</option>
  <option value="Gold">Gold</option>
  <option value="Platinum">Platinum</option>
  <option value="Hidden">Hidden</option>
</select>
Points: <input name="tr[3][ach_value]" type="text" size="4" maxlength="4" /><p></p>

Trophy Name:    <input name="tr[4][name]" type="text" size="50" maxlength="255" /><br/>
Descripton:     <input name="tr[4][desc]" type="text" size="50" maxlength="255" /><br/>
Trophy Color:   <select name="tr[4][color]">
  <option value="Bronze">Bronze</option>
  <option value="Silver">Silver</option>
  <option value="Gold">Gold</option>
  <option value="Platinum">Platinum</option>
  <option value="Hidden">Hidden</option>
</select>
Points: <input name="tr[4][ach_value]" type="text" size="4" maxlength="4" /><p></p>

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

您的PHP代码现在应该如下所示:

// $_POST['tr'] holds an array with trophies and the trophies' attributes

foreach($_POST['tr'] as $tr) {

  /*
    $tr is now a hash like:

    array( 
      "name" => "a name you entered",
      "desc" => "the description",
      "color" => "black",
      "ach_value" => "a value you entered"
    )

  */

  if(empty($tr['name'])) { continue; }

  $sql3 = "INSERT INTO trophies (game_name, tr_name, tr_description, tr_color, tr_ach_value) VALUES ('".$_POST['game_name']."', '".$tr['name']."', '".$tr['desc']."', '".$tr['color']."', '".$tr['ach_value']."')";


  mysql_query($sql3);
}