...

View Full Version : using field counts



o0O0o.o0O0o
08-07-2008, 02:25 AM
hi ,

i have this code



while ($row = mysql_fetch_assoc($result))

{
$newArray[0][] = $row['orderNumber'];

$newArray[1][] = $row['name'];

$newArray[2][] = $row['productName'];
$newArray[3][] = $row['subproduct'];
$newArray[4][] = $row['count'];
$newArray[5][] = $row['run'];
}


return $newArray;
}



I want the way where i don't have to mention everytime the number of fields and their names. Something that count the fields and their values automatically and put in array. something like below




$field_count = mysql_num_fields($result);
$i = 0;

while ($i < $field_count)
{
$field = mysql_fetch_field($result);
$Arr[$i] = $field[name_of_field];
$arr[$i][$i] = $fields[$i]);
$i++;
}

return $arr


so that $arr[0] contain the name of field like 'orderNumber'
$arr[0][0] contain the order '2333333'

oesxyl
08-07-2008, 04:06 AM
$fields = array(0 => 'orderNumber',
1 => 'name',
2 => 'productName',
3 => 'subproduct',
4 => 'count'
5 => 'run']);

while ($row = mysql_fetch_assoc($result)){
foreach($fields as $i => $name){
$newArray[$i] = $row[$fields[$i]];
}
}
return $newArray;
}

regards

o0O0o.o0O0o
08-07-2008, 05:54 AM
$fields = array(0 => 'orderNumber',
1 => 'name',
2 => 'productName',
3 => 'subproduct',
4 => 'count'
5 => 'run']);

I don't want these to define as well . the system should get these from the mysql resultset columns-name. like mysql_field_name($count,$i);

oesxyl
08-07-2008, 06:02 AM
I don't want these to define as well . the system should get these from the mysql resultset columns-name.


$query = "desc yourtablenamehere";
$res = mysql_query($query);
if($res){
$fields = array();
while($fld = mysql_fetch_assoc($res)){
$fields[] = $fld['Field'];
}
if(!empty($fields)){
// rest of the posted code here
}else{
// errors
}
}else{
// errors
}


wait! do you want to get the fields name from your query?
regards

o0O0o.o0O0o
08-07-2008, 07:21 AM
I want to display the report so the table column caption should be the name of the fields in mysql table.

I just want to pass the query to the function like


getReport("Select * from tblusers");

then system should get do evrything by self. I had done this before but not able to do it now

oesxyl
08-07-2008, 07:50 AM
I want to display the report so the table column caption should be the name of the fields in mysql table.

I just want to pass the query to the function like


getReport("Select * from tblusers");

then system should get do evrything by self. I had done this before but not able to do it now



function getReport($query){
$res = mysql_query($query);
if($res){
$report = array();
$reporthead = array();
while($row = mysql_fetch_assoc($res)){
if(empty($reporthead)){
$reporthead = array_keys($row);
}
foreach($reporthead as $fieldname){
$report[$fieldname][] = $row[$fieldname];
}
}
return $report;
}
return false;
}


regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum