我正在尝试显示单行值,但它显示多个值

时间:2014-10-16 07:01:41

标签: php

当我尝试使用explode从数据库中检索值时,但是当我使用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'];
$qt=@explode(',',$qty);
$un=$row_temp['unit'];
$unit=@explode(',',$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 info@technopia.in
/*****************************************************************************/
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 = 'Yogesh@123';
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 个答案:

答案 0 :(得分: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'];
$qt=@explode(',',$qty);
$un=$row_temp['unit'];
$unit=@explode(',',$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>