2014-10-16 3 views
0

폭발을 사용하여 데이터베이스에서 값을 검색하려고하지만 foreach 루프를 사용하는 동안 적절한 결과를 얻지 못하는 경우. 각 행은 여러 시간을 표시합니다. 그래서 내 문제를 정리해주세요.단일 행 값을 표시하려고하지만 여러 값을 표시합니다.

index.php를

<?php 
require_once('DB.class.php'); 

$db = new DB(); 
$db->connect(); 
?> 

<HTML> 
<HEAD> 
<TITLE>PHP jQuery Dynamic Textbox</TITLE> 
<LINK href="style.css" rel="stylesheet" type="text/css" /> 


</HEAD> 
<BODY> 
<FORM name="frmProduct" method="post" action=""> 
<div id="output1"> 
<legend>Product List</legend> 
<table width="90%" align="center" class="table table-bordered table-hover"> 
<tr> 
<th>item Name</th> 
<th>item Price</th> 
<th>Qty</th> 
<th>Unit</th> 

</tr> 
<?php 
$total=0; 

$templist=$db->SelectTable("item","",""); 
if(count($templist)>0) { 
foreach($templist as $row_temp) 
{ 
?> 
<tr> 

<?php 

$qty=$row_temp['qty']; 
[email protected](',',$qty); 
$un=$row_temp['unit']; 
[email protected](',',$un); 
foreach($qt as $q => $value){ 
foreach($unit as $uni => $value1){ 

?> 
<td class="righr"><?=$row_temp['item_name'];?></td> 
<td class="right"><?=$row_temp['item_price'];?></td> 
<td class="right"><?=$value?></td> 
<td class="right"><?=$value1?></td> 

<td align="center"><input type="button" name="Delete" Value="Delete" onclick="showtempdata(<?=$row_temp['temp_id']?>);" class="btn btn-danger" /></td> 
</tr> 
<?php 
} 
} 
//$mask=$mask[1]; 
} 
?> 

<?php 
}else{ 
?> <tr><td colspan="10"><label class="text-error" style="text-align:center"> Product Not added</label></td></tr> 
<?php }?> 
</table> 
</form> 
</BODY> 
</HTML> 

DB.class.php

<?php 
//DB.class.php 
/***************************************************************************** 
/*Copyright (C) 2013 Narwade Jaywant 
For any details please feel free to contact me at [email protected] 
/*****************************************************************************/ 
class DB { 

protected $db_name = 'ajaxphp'; 
protected $db_user = 'root'; 
protected $db_pass = ''; 
protected $db_host = 'localhost'; 
/*protected $db_name = 'infortte_yogesh'; 
protected $db_user = 'infortte_yogesh'; 
protected $db_pass = '[email protected]'; 
protected $db_host = 'localhost';*/ 

//Constructor is called whenever a new object is created. 
function __construct() { 
$connection = mysql_connect($this->db_host, $this->db_user, $this->db_pass); 
mysql_select_db($this->db_name); 

return true; 
} 
public function connect(){ 
$connection = mysql_connect($this->db_host, $this->db_user, $this->db_pass); 
mysql_select_db($this->db_name); 

return true; 
} 
//takes a mysql row set and returns an associative array, where the keys 
//in the array are the column names in the row set. If singleRow is set to 
//true, then it will return a single row instead of an array of rows. 
public function processRowSet($rowSet, $singleRow=false) 
{ 
$resultArray = array(); 
while($row = mysql_fetch_assoc($rowSet)) 
{ 
array_push($resultArray, $row); 
} 

if($singleRow === true) 
return $resultArray[0]; 

return $resultArray; 
} 

//Select rows from the database. 
//returns a full row or rows from $table using $where as the where clause. 
//return value is an associative array with column names as keys. 
public function SelectTable($table, $where="",$fieldarray="",$debug="") { 
if ($fieldarray=="") 
{ 
$f_list = "*"; 
} 
else 
{ 
$f_list = $fieldarray ; 
} 
$sql = "SELECT $f_list FROM $table "; 
if( ! empty($where)) 
$sql .= " WHERE $where"; 

if($debug==1){echo $sql;exit();} 
$result = mysql_query($sql); 
if(! $result) 
return 0; 
return $this->processRowSet($result); 
} 
public function SelectSingle($table, $where,$fieldarray="",$debug="") { 
if ($fieldarray=="") 
{ 
$f_list = "*"; 
} 
else 
{ 
$f_list = $fieldarray ; 
} 
$sql = "SELECT $f_list FROM $table "; 
if( ! empty($where)) 
$sql .= " WHERE $where"; 

if($debug==1){echo $sql;exit();} 
$result = mysql_query($sql); 
if(! $result) 
return 0; 
if(mysql_num_rows($result) == 1) 
return $this->processRowSet($result, true); 
} 
//Updates a current row in the database. 
//takes an array of data, where the keys in the array are the column names 
//and the values are the data that will be inserted into those columns. 
//$table is the name of the table and $where is the sql where clause. 
public function Update($table,$where,$data,$debug="") { 

foreach ($data as $column=>$value) 
{ 
if($value !="now()"){ 
$fv[] = "$column = \""."$value"."\""; 
}else{ 
$fv[]= "$column = "."$value".""; 
} 
} 
$fv_list = trim(implode(", ", $fv)); 

$sql = "UPDATE $table SET "."$fv_list"." WHERE $where"; 
if($debug==1){echo $sql;exit();} 
mysql_query($sql) or die(mysql_error()); 
return true; 
} 

//Inserts a new row into the database. 
//takes an array of data, where the keys in the array are the column names 
//and the values are the data that will be inserted into those columns. 
//$table is the name of the table. 
public function Insert($table,$data,$debug="") { 

$columns = ""; 
$values = ""; 
foreach($data as $column=>$value) 
{ 
$field[] = $column; 
if($value !="now()") 
$values[] = "'$value'"; 
else 
$values[] = "$value"; 
} 
$columns = trim(implode(", ", $field)); 
$values = trim(implode(", ", $values)); 

$sql = "insert into $table ($columns) values ($values)"; 
if($debug==1){echo $sql;exit();} 
mysql_query($sql) or die(mysql_error()); 

//return the ID of the user in the database. 
return mysql_insert_id(); 

} 
public function Delete($table, $condition) 
{ 

$query = "DELETE FROM $table WHERE $condition"; 
$result = mysql_query($query); 
if(! $result) 
return 0; 
return 1; 
} 

} 
?> 

데이터베이스

CREATE TABLE IF NOT EXISTS `item` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `item_name` varchar(255) NOT NULL, 
    `item_price` int(11) NOT NULL, 
    `unit` varchar(11) NOT NULL, 
    `qty` varchar(11) NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ; 

INSERT INTO `item` (`id`, `item_name`, `item_price`, `unit`, `qty`) VALUES 
(1, 'hello', 21, '1,2,3', '11,12,13'), 
(2, 'hi', 22, '4,5,6', '67,8'), 
(3, 'test', 344, '0', ''), 
(4, 'wow', 444, '0', ''); 
+1

안녕, Y ou는 같은 행을 가지지 만'$ unit'은 행마다 다릅니다. 이것을 쉽게하기 위해 지금 얻고있는 출력과 예상 출력을 게시 할 수 있습니까? – vbrmnd

답변

0

는 시도이

<?php 
require_once('DB.class.php'); 

$db = new DB(); 
$db->connect(); 
?> 

<HTML> 
<HEAD> 
<TITLE>PHP jQuery Dynamic Textbox</TITLE> 
<LINK href="style.css" rel="stylesheet" type="text/css" /> 


</HEAD> 
<BODY> 
<FORM name="frmProduct" method="post" action=""> 
<div id="output1"> 
<legend>Product List</legend> 
<table width="90%" align="center" class="table table-bordered table-hover"> 
<tr> 
<th>item Name</th> 
<th>item Price</th> 
<th>Qty</th> 
<th>Unit</th> 

</tr> 
<?php 
$total=0; 

$templist=$db->SelectTable("item","",""); 
if(count($templist)>0) { 
foreach($templist as $row_temp) 
{ 
?> 
<tr> 

<?php 

$qty=$row_temp['qty']; 
[email protected](',',$qty); 
$un=$row_temp['unit']; 
[email protected](',',$un); ?> 
<td class="righr"><?=$row_temp['item_name'];?></td> 
<td class="right"><?=$row_temp['item_price'];?></td> 
<td class="right"><?php foreach($qt as $q => $value){?><?=$value?><?php }?></td> 
<td class="right"><?php foreach($unit as $uni => $value1){?><?=$value1?><?php }?></td> 

<td align="center"><input type="button" name="Delete" Value="Delete" onclick="showtempdata(<?=$row_temp['temp_id']?>);" class="btn btn-danger" /></td> 
</tr> 

<?php 
} 
}else{ 
?> <tr><td colspan="10"><label class="text-error" style="text-align:center"> Product Not added</label></td></tr> 
<?php }?> 
</table> 
</form> 
</BODY> 
</HTML> 
+0

고맙습니다.하지만 같은 행에있는 유일한 표시는 다음과 같습니다. 단 행과 그 값 21과 4를 다음 행 5와 같이 다음 행에 표시하고 나누기를 원합니다. 이와 같이 내가 원하는 것입니다. –

+0

설명해 주시겠습니까? 내가 표 형식으로 어떻게 보이게해야 하는가? –

+0

내 결과를 원하는 이런 품명 상품 가격 단위 수량 헬로 21 1 11 2 12 \t \t \t 3~13 추천 –

관련 문제