|
 |
|
ขอถามเรื่องการ insert ข้อมูลแบบวนลูปด้วย foreach คือผมมีเรื่องอยากถามครับ |
|
 |
|
|
 |
 |
|
เอาโค้ดมาดูด้วยดีไหมคะ
ถ้าจำไม่ผิด foreach มันจะ loop ดาต้าออกมาจาก array ทีละตัว ถ้ามองในมุมฐานข้อมูลก็คือ ทีละคอลัมน์หรือฟิวด์
ที่คุณต้องทำคือ ให้มัน loop record ด้วยค่ะ ส่วนใหญ่จะนิยม ใช้ while มากกว่า
|
 |
 |
 |
 |
Date :
2010-03-22 14:52:39 |
By :
ultrasiam |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมคิดว่าforeach มันดึงข้อมูลมาแบบอาเรย์ ก็น่าจะเพิ่มข้อมูลได้อ่ะครับ
แสดงว่าต้องใช้ while เสริมด้วยใช้มั้ยครับ - -a
ขอบคุณมากครับ
|
 |
 |
 |
 |
Date :
2010-03-22 15:07:51 |
By :
rakket |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอาโค้ดมาดูด้วยชัวร์กว่าค่ะ จินตนาการแบบนี้อาจจะเข้าใจไปคนละทางได้
|
 |
 |
 |
 |
Date :
2010-03-22 15:09:50 |
By :
ultrasiam |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ต้องดูด้วยว่าตอนคุณดึงข้อมูลมาเป็น array นั้น ดึงมาแบบไหน มาหมดทุก record หรือว่ามาแค่ record เดียว
|
 |
 |
 |
 |
Date :
2010-03-22 15:11:27 |
By :
ultrasiam |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คือ ผมตอนแรกผม ประกาศให้ เรียกใช้ ฟังชั่นนี้
Code (PHP)
function getListquestionpage()
{
global $conn;
//$sql = "SELECT c.id as qid, name, question_id, description, fullsccres FROM question_categories_tb as c INNER JOIN question as q ON c.id = question_id "
//$result = &$conn->Execute( "SELECT c.id as cid, c.name, c.question_id, c.fullsccres , q.id as qid,q.question_id, q.title, q.fullsccres FROM question_categories_tb as c INNER JOIN question a q ON c.id = q.question_id ");
$conn -> Execute ( "set names 'utf8'");
$result = &$conn->Execute( "SELECT q.id as questionid, c.id as parentid, q.title, q.fullsccres as qscore, c.name, c.fullscores as cscore FROM question_tb as q INNER JOIN question_categories_tb as c ON q.question_id = c.id ORDER BY c.id");
while (!$result->EOF) {
$thisref[$result->fields['parentid']]['id'] = $result->fields['parentid'];
$thisref[$result->fields['parentid']]['name'] = $result->fields['name'];
$thisref[$result->fields['parentid']]['cscore'] = $result->fields['cscore'];
$arr_questions['title'] = $result->fields['title'];
$arr_questions['qscore'] = $result->fields['qscore'];
$thisref[$result->fields['parentid'] ]['questions'][ $result->fields['questionid'] ] = $arr_questions;
$result->MoveNext();
}
return $thisref;
}
function getListbranch()
{
global $conn;
$result = &$conn->Execute( "SELECT * FROM brance_tb");
while (!$result->EOF) {
if($result->fields['branceh_id'] > 0) {
$refs[$result->fields['branceh_id']]['id'] = $result->fields['branceh_id'];
$refs[$result->fields['branceh_id']]['name'] = $result->fields['branceh_name'];
$refs[$result->fields['branceh_id']]['bm_id'] = $result->fields['bm_id'] ;
$refs[$result->fields['branceh_id']]['email'] = $result->fields['email'] ;
$refs[$result->fields['branceh_id']] ['address'] = $result->fields['address'] ;
$refs[$result->fields['branceh_id']]['tel'] = $result->fields['tel'] ;
}
$result->MoveNext();
}
return $refs;
}
เวลาเรียกใช้
Code (PHP)
$r = getListquestionpage();
$a = getListbranch();
จากนั้น ผมก็เขียน foreach เพื่อเรียกใช้
Code (PHP)
<?php
$i=1;
foreach($r as $key => $value){
?>
<tr class="style16">
<td width="44" height="33" class=""><div align="left" class="style19">
<div align="center" class="s"><?php echo $i;?>
<input type="hidden" name="scoretest1[<?php echo $key ?>]" id="scores" value="<?php echo $i;?>" />
</div>
</div></td>
<td width="618" align="center" class=""><table cellspacing="0" cellpadding="0">
<tr>
<td height="24" align="left" width="571"> <?php echo $value['name'] ?></td>
</tr>
</table> </td>
<td width="42"><div align="center" class="style19">
<div align="center" class="style16">
<?php echo $value['cscore'] ?>
<input type="hidden" name="scoretest[<?php echo $key ?>]" id="score" value="<?php echo $value['cscore'] ?>" />
</div>
</div></td>
<td colspan="2" class="style16"><div align="center" class="style19">
<div align="center" class="style16">ได้</div>
</div></td>
</tr>
<?php
$x=1;
foreach($value['questions'] as $key2 => $value2){
?>
<tr class="style23">
<td class="style23"><div align="right"><span class="s">
<input type="hidden" name="scores2" id="scores2" value="<?php echo $x;?>" />
</span><?php echo $x ?></div></td>
<td class="style23">
<table cellspacing="0" cellpadding="0">
<tr>
<td height="23" align="left" width="571"><?php echo $value2['title'] ?></td>
</tr>
</table></td>
<td class="style21"><div align="center"><?php echo $value2['qscore'] ?></div></td>
<td colspan="2" class="style21"><div align="center">
<input type="checkbox" name="checkbox2[<?php echo $key2 ?>]" id="checkbox2[<?php echo $key2 ?>]" value="<?php echo $value2['qscore'] ?>" />
</div></td>
</tr>
<?php $x++;
}
$i++;
}
?>
แล้วก็ให้มันอยู่ในรูปแบบ $_POST เพื่อนำไปลงฐานข้อมูล
Code (PHP)
if($_POST){
foreach($_POST['scoretest'] as $id => $sum2){
$fullscore += $sum2 ;
}
foreach($_POST['checkbox2'] as $key => $sum){
$sumscore += $sum ;
}
$gscore = ($sumscore*100)/$fullscore;
$bmid = insertUserbm(StripExtraSpace($_POST['name']),$_POST['position']);
$evid = insertEvaluate($bmid, $_POST['Branch'], $fullscore, $sumscore, $gscore,$_POST['date'],$_POST['time']);
สรุปคือผมต้องการที่จะกลับมาตรวจสอบใหม่ได้ โดยที่เหมือนต้นฉบับเวลา insert เพื่อเอาไปทำเป็น Report ผมจึงจำเป็นต้อง insert แบบลูป ซึ่งไม่แน่ใจว่าใช้ foreach รึเปล่า ยังไงก็ให้คำแนะนำด้วยนะครับ
ติดที่เดียว T^T ขอโทดนะครับที่ถามซับซ้อน พึ่ง เรียน php มา 3 อาทิตย์ เลยทำให้บางอย่างยังไม่รู้
|
 |
 |
 |
 |
Date :
2010-03-24 16:43:15 |
By :
rakket |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
- - - - - - - - - - -สงสัยคงไม่ได้ จริงๆ มั้ง 
|
 |
 |
 |
 |
Date :
2010-03-25 09:23:46 |
By :
rakket |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใครพอรู้มั้ง ก็ช่วย อธิบายหน่อย สิ ไม่ต้องเเอาของผมก็ได้ แต่ขอใกล้เคียง แบบคร่าวๆ
*ตอนนี้ผมข้ามไปทำฟอร์มที่2 แล้ว จอยข้อมูลกัน 5 ตาราง สนุกจิงๆ
|
 |
 |
 |
 |
Date :
2010-03-25 11:42:51 |
By :
rakket |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมไม่ได้อ่านโค้ดนะครับ ตาลาย 
จะบันทึกอะไรในลูปก็บันทึกได้ตามสบายคับ เช่น
$a[] = 'a';
$a[] = 'b';
foreach ($a as $key => $val)
{
mysql_query("INSERT INTO ur_table SET col1=$key, col2='$val'");
}
แค่นี้ก็บันทึกไปสองเรคคอร์ดแล้วคับ
|
 |
 |
 |
 |
Date :
2010-03-25 12:14:30 |
By :
pjgunner |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ครับ ขอบคุณมากครับ แล้วผมจะนำไปประยุก ใช้ครับ
|
 |
 |
 |
 |
Date :
2010-03-25 13:07:24 |
By :
rakket |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
<input type="text" name="a[]" size="10"/>
<input type="text" name="b[]" size="10"/>
foreach($_POST['dimension'] as $key=>$val)
{
$sql2 = "insert into table(a,b) values ('".$_POST[a][$key]."',".$_POST[b][$key]."')";
mssql_query($sql2);
}
|
 |
 |
 |
 |
Date :
2010-03-25 13:48:38 |
By :
paphun |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 01
|