bulrush Posted May 17, 2010 Share Posted May 17, 2010 I have a function in an include file which, when passed a query, and an array of column names, executes the query and displays the data matched in the query. However, the last row of the query is always displayed twice. Can anyone help me find out why? Thanks. //==================================================== function crDispSelect($query, $fieldarr, $booldie=false) /* $query=full, valid SELECT query. $fieldarr=contains array of fields to display in a table. Field column order is order of this array. These field names must appear in $query. */ { global $dbc; //This is the database connect variable. $query=trim($query); if (empty($query)) { $s='param 1 query is empty.'; crError(__FUNCTION__,$s,true); //Display error and die(). } if (count($fieldarr)==0) { $s='param 2 fieldarr is empty.'; crError(__FUNCTION__,$s,true); } //Do query and error checking here. if (!$result=mysqli_query($dbc,$query)) { $msg=mysql_error(); crError(__FUNCTION__,$msg.'<br/>'.$query,true); } $num=mysqli_num_rows($result); if ($num==0) { $msg='No data found. '.$query; crError(__FUNCTION__,$msg,false); } //$s='num rows='.$num; //crDebug($s); //DEBUG //Do loop here to construct options. $cnt=count($fieldarr); //Save number of fields. $s='<table border="1">'; echo "$s\n"; $s='<tr>'; for ($i=0; $i<$cnt; $i++) //Make column headers. { $s.='<td><b>'.$fieldarr[$i].'</b>'; } //for i echo "$s\n"; while ($row = mysqli_fetch_array($result)) { //Loop through each field in each row. $s='<tr>'; for ($i=0; $i<$cnt; $i++) { $n=$row[$fieldarr[$i]]; //Get value of each field for this row. $s.='<td>'.$n; } echo "$s\n"; } //while $s.='</table>'; echo "$s\n"; return; //crDispSelect } //==================================================== function crError($func, $myerr, $bdie=false) /* Display error, set $b to true to die. $func=function name, must be passed in from calling program. $myerr=error message $bdie=true to die after the error. */ { $msg=mysql_error(); echo '<p class="errormsg">ERROR in '.$func.': '.$myerr.'</p>'; if ($bdie) { die(); } return; } Quote Link to comment https://forums.phpfreaks.com/topic/202045-last-row-appears-twice-in-query/ Share on other sites More sharing options...
bulrush Posted May 17, 2010 Author Share Posted May 17, 2010 Anyone? This is a mystery to me. Quote Link to comment https://forums.phpfreaks.com/topic/202045-last-row-appears-twice-in-query/#findComment-1059686 Share on other sites More sharing options...
DavidAM Posted May 17, 2010 Share Posted May 17, 2010 $s.='</table>'; echo "$s\n"; the dot-equal is adding the closing table tag to the current contents of $s. At this point $s still contains the last row from the while loop, so you are outputting it again. This line should be: $s ='</table>'; echo "$s\n"; although, I would just echo '</table>'; Also, you are not closing any of your columns or rows. You need to add a '</td>' after each cell content and a '</tr>' after the last cell (closing tag) of each row. Some browsers might not render content that follows the table correctly if things are not closed properly. One more thing, instead of using '<td><b>Column Head</b></td>' try using '<thead><th>Column Head</th></thead>'. This does a couple of things: 1) It bolds the contents of the cell so you don't have to; 2) it centers the contents; 3) some browsers will repeat the heading row(s) when a page is printed and runs over to multiple pages. Quote Link to comment https://forums.phpfreaks.com/topic/202045-last-row-appears-twice-in-query/#findComment-1059704 Share on other sites More sharing options...
bulrush Posted May 17, 2010 Author Share Posted May 17, 2010 Thank you for your tips. I will use <thead> and do the other tips as well. Quote Link to comment https://forums.phpfreaks.com/topic/202045-last-row-appears-twice-in-query/#findComment-1059738 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.