Array ile PHP Değişken

1 Cevap php

i'm trying to make a "update user's power" page. It is something similar to those you can find in say, invisionfree forums.

I need it to generate a list of members with checkbox [done] Add an option for it [done]

Ne yapmam nasıl bilmiyorum, güncellemek için söylemek, seçilen tüm kullanıcılara seçilen güç vermek.

Sonra ben bir şey aramaya gitti ve çoğu bunu yapmak için bir dizi kullanır buldum, ama aslında nasıl çalıştığını açıklar birini bulmadım.

I (kendi değişiklik ile) aldı örnek bu oldu:

while($row = mysql_fetch_array($result))
{
echo    '<tr>'.$id[]=$rows['id'].'';
echo    '<td width="50px" align="center" class="TableFormCell"><input type="checkbox" name="option[]" /></td>';
echo    '<td width="170px" align="center" class="TableFormCell">'.$row['uname'].'</td>';
echo    '</tr>';
}

I'm not sure what exactly $id[]=$rows['id'] does

Benim opsiyon [] [1] seçeneği bir dizi olacak, satırdan sonra biliyorum, opsiyon [2], opsiyon [3]

ne güç verilmelidir için, ben bu konuda bir sorunum yok ama ben hiçbir ipucu kulüpler var veritabanını güncellemek için nasıl ettik ...

for($i=0;$i<$count;$i++){
$sql1="UPDATE ninos SET power='$power' WHERE id='$option[$i]'";
$result1=mysql_query($sql1);
}

Yani ben 5 kullanıcı, Aye, Bee, Cee, Dee, Eee benim komut dosyası çalıştırmak gibi olurdu böylece 1,2,3,4,5 ve kimlikleri ile nasıl ben bunu yapabilirsiniz var Say

$sql1="UPDATE ninos SET power = '$power' Where id='1','2','3','4','5'";

, Teşekkürler yardım edin.

Update

nuqqsa için

Here's the selecting page

$result = mysql_query("SELECT * FROM ninos");
while($row = mysql_fetch_array($result))
  {
echo    '<tr>';
echo    '<td width="50px" align="center" class="TableFormCell"><input     type="checkbox" name="option[]" value="' . $row['id'] . '" /></td>';
echo    '<td width="170px" align="center" class="TableFormCell">'.$row['uname'].'</td>';
echo    '</tr>';
}
?>

İşte güncelleme sayfası

<?php
include('../openconn.php');

$power = $_POST['power'];

$ids = array();
foreach($_POST['option'] as $id)
{
$ids[] = (int)$id;
}   

if(!empty($ids)) {
// if there's at least one selected user
$sql1 = "UPDATE ninos SET power = '$power' Where id IN (" . implode(',', $ids) . ")";
// execute the query (...)
}


include('../closeconn.php');
?>

1 Cevap

Öncelikle, bu çok mantıklı değil (atama yazdırılabilir değil çıkışı şey yapar):

echo '<tr>'.$id[]=$rows['id'].'';

Bu aynı işi yapar ama (bir döngü içinde, bir dizideki tüm kimlikleri saklamak devam edecek, çünkü ilk satırı) bu çok daha net:

$id[] = $rows['id'];
echo '<tr>';

UPDATE: Anyways there's no need to store the ids in an array here, just use $row['id'] to print the user identifier in the option value (right now no value is set):

while($row = mysql_fetch_array($result))
{
echo    '<tr>';
echo    '<td width="50px" align="center" class="TableFormCell"><input type="checkbox" name="option[]" value="' . $row['id'] . '"/></td>';
echo    '<td width="170px" align="center" class="TableFormCell">'.$row['uname'].'</td>';
echo    '</tr>';
}

Form action script seçilen kimlikleri içeren bir $_POST['option'] değişkeni alacaksınız. Bu kullanılabilecek kadar bir örnek:

$ids = array();
// input filtering: convert all values to integers
foreach($_POST['option'] as $id)
{
    $ids[] = (int)$id;
}    
if(!empty($ids)) {
    // if there's at least one selected user
    $sql1 = "UPDATE ninos SET power = '$power' Where id IN (" . implode(',', $ids) . ")";
    // execute the query (...)
}