Reputation: 3
I'm fetching data from database and suppose there is 23 columns in a table then I have to write 23 lines to set value. Is there any other short way to do this?
In the example below there are only 15 columns and I wrote 15 lines. any short way?
while( $row = mysql_fetch_array( $export ) )
{
$line = '';
$line.=setValue($row[0]);
$line.=setValue($row[1]);
$line.=setValue($row[2]);
$line.=setValue($row[3]);
$line.=setValue($row[4]);
$line.=setValue($row[5]);
$line.=setValue($row[6]);
$line.=setValue($row[7]);
$line.=setValue($row[8]);
$line.=setValue($row[9]);
$line.=setValue($row[10]);
$line.=setValue($row[11]);
$line.=setValue($row[12]);
$line.=setValue($row[13]);
$line.=setValue($row[14]);
}
Upvotes: 0
Views: 105
Reputation: 48357
While you could do something like....
while ($row=mysql_fetch_array($export)) {
$line=array();
$line=array_map('setValue',$xport);
$line=implode('',$line);
}
....its a very messy construct - it presupposes that the query will return a very well defined resultset, and the structure of the resultset and its subsequent application are diplaced; as a general rule of thumb, you should never use mysql_fetch_array - use mysql_fetch_assoc instead.
Upvotes: 0
Reputation: 151
while( $row = mysql_fetch_array( $export ) )
{
$line = '';
for($i = 0; $i < 15; $i++) {
$line .= setValue($row[$i]);
}
}
That's one approach. There are others. You can replace the '15' with a count form the returned array. This way the for loop will iterate until the end of the array.
Ups, lol, too late...
Upvotes: 0
Reputation: 30385
I am not really sure if this is what you are looking for but you can use a for loop like this:
while( $row = mysql_fetch_array( $export ) )
{
$line = '';
for($i = 0; $i < 15; $i++)
{
$line.=setValue($row[$i]);
}
}
Upvotes: 0
Reputation: 212412
while( $row = mysql_fetch_array( $export ) )
{
$line = '';
foreach($row as $value) {
$line.=setValue($value);
}
}
Upvotes: 6