...

View Full Version : Check if last row is read help??



dajohnson1s
03-09-2009, 07:56 PM
I am dynamically creating a php page that will build an array from the database. What I would like to do is add a comma after each row, except for the last one.

I have looked for a function to do this, and I had even thought I could just check if there is a next row. Not sure what to do.

Here is what my code looks like:


$Data = "<\?php \n";
fwrite($Handle, $Data);
$Data = "\$q = strtolower(\$_GET[\"q\"]); \n";
fwrite($Handle, $Data);

$Data = "if (!\$q) return; \n";
fwrite($Handle, $Data);

$Data = "\$items = array( \n";
fwrite($Handle, $Data);




$i = 1;
while ($row = mysql_fetch_assoc($result)) {
$Data = " \"$i\"=>\"$row[common_name]\" \n";
fwrite($Handle, $Data);
$i++;
}



$Data = "); \n";
fwrite($Handle, $Data);
$Data = "foreach (\$items as \$key=>\$value) { \n";
fwrite($Handle, $Data);
$Data = " if (strpos(strtolower(\$value), \$q) !== false) { \n";
fwrite($Handle, $Data);
$Data = " echo \"\$value\n\"; \n";
fwrite($Handle, $Data);
$Data = " } \n";
fwrite($Handle, $Data);
$Data = "} \n";
fwrite($Handle, $Data);
$Data = "?> \n";
fwrite($Handle, $Data);


There is some error checking for the file opening and database connection as well.

This seems so simple, its not funny.

Thanks

abduraooft
03-09-2009, 09:16 PM
Not sure, but you'd need to use a join() like

while ($row = mysql_fetch_assoc($result)) {
$Data[] = " \"$i\"=>\"$row[common_name]\" \n";

$i++;
}

$Data=join(',',$Data)
fwrite($Handle, $Data);

dajohnson1s
03-10-2009, 04:08 AM
abduraooft,

Thanks for the suggestion, but I get an error about [] doesn't like strings.

I had thought I could look at the last item in the array, but its not an array. So I am not sure, maybe I should build all the values into an array, then output them.

Thanks again,



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum