ayanda83 Posted August 3, 2014 Share Posted August 3, 2014 hi there, the code below is suppose to display something like the attachement "code2" but instead it displays something like attachement "code1" please assist in find what is wrong with my echo lines. <?php $counter = 2; $sqlq="select * from state WHERE status = 0 "; $categorysqlq = mysql_query($sqlq); $varq = mysql_num_rows($categorysqlq); while($catfetchq = mysql_fetch_array($categorysqlq)) { $cnty = $catfetchq[0]; $sqllq="select * from vehicle WHERE country = '$cnty' "; $categorysqllq = mysql_query($sqllq); $numsql = "select * from branchaddr WHERE state = '$cnty' "; $numquery = mysql_query($numsql); $varqa = mysql_num_rows($numquery); $cntyfetchq= mysql_fetch_array($numquery); if($varq != 0){ if($counter == 2){ echo "<tr><td><a href=\"transport2.php?id=".$cntyfetchq['state'].">".$catfetchq[1]."(<span style=\"color:red\">".$varqa."</span>)</a></td>"; $counter--; } else{ echo "<td><a href=\"transport2.php?id=".$cntyfetchq['state'].">".$catfetchq[1]."(<span style=\"color:red\">".$varqa."</span>)</a></td></tr>"; $counter = 2; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/290245-unable-to-display-output-properly-using-php-echo-and-html-tags/ Share on other sites More sharing options...
requinix Posted August 3, 2014 Share Posted August 3, 2014 The a href values are missing the ending quotes. Quote Link to comment https://forums.phpfreaks.com/topic/290245-unable-to-display-output-properly-using-php-echo-and-html-tags/#findComment-1486727 Share on other sites More sharing options...
ayanda83 Posted August 3, 2014 Author Share Posted August 3, 2014 thank you for responding requinix, but can you be a little more descriptive in your answer because I thought the end quote in the next statement was the end quote for the href values ['state']." Â or please show me using the line below where you would put the end quote for href. Â echo "<tr><td><a href=\"transport2.php?id=".$cntyfetchq['state'].">".$catfetchq[1]."(<span style=\"color:red\">".$varqa."</span>)</a></td>"; Quote Link to comment https://forums.phpfreaks.com/topic/290245-unable-to-display-output-properly-using-php-echo-and-html-tags/#findComment-1486729 Share on other sites More sharing options...
Solution Ch0cu3r Posted August 3, 2014 Solution Share Posted August 3, 2014 (edited) You are getting that result because you have an error in the HTML code being outputted. The cause of the error is leaving off the closing quote for the href attribute value, as highlighted below echo "<tr><td><a href=\"transport2.php?id=".$cntyfetchq['state']."\">".$catfetchq[1]."(<span style=\"color:red\">".$varqa."</span>)</a></td>"; Â NOTE: Running queries within loops is not recommended. If you have normalized your table data you should be able to get the data you require (if the data relates) from multiple tables using JOINS. Example query SELECT s.state, COUNT( b.state ) AS stateCount FROM state AS s LEFT JOIN branchaddr b ON b.state = s.state WHERE s.status =0 GROUP BY s.state ORDER BY s.state The above query fetches all states in the state table and also returns a running total of each state used in the branchaddre table. Â You can then process the query results using // one query used to get the data require (the state name and state count) $sqlq = " SELECT s.state, COUNT( b.state ) AS stateCount FROM states AS s LEFT JOIN branchaddr b ON b.state = s.state WHERE s.status =0 GROUP BY s.state ORDER BY s.state"; $states = mysql_query($sqlq); $i = 0; // a counter - used in the while loop // start table and open new row echo "<table border=1><tr>"; while(list($state, $stateCount) = mysql_fetch_row($states)) { // echo table cell printf('<td><a href="transport2.php?id=%s">%1$s(<span style="color:red">%s</span>)</a></td>', $state, $stateCount); // closes current row and opens a new row every 2 columns if(++$i % 2 == 0) echo '</tr><tr>'; } // close current row and table echo '</tr></table>'; This should output the states in a table like your second image Edited August 3, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/290245-unable-to-display-output-properly-using-php-echo-and-html-tags/#findComment-1486730 Share on other sites More sharing options...
ayanda83 Posted August 3, 2014 Author Share Posted August 3, 2014 Thank you very very much Ch0cu3r, You have just solved something I have been battling with for 2 days. thank you again. Quote Link to comment https://forums.phpfreaks.com/topic/290245-unable-to-display-output-properly-using-php-echo-and-html-tags/#findComment-1486731 Share on other sites More sharing options...
davidannis Posted August 3, 2014 Share Posted August 3, 2014 Thank you very very much Ch0cu3r, You have just solved something I have been battling with for 2 days. thank you again. You should mark the issue solved - there will be a button on Ch0cu3r's post that says "mark sloved" and maybe hit Like on the posts of those who have helped you. Quote Link to comment https://forums.phpfreaks.com/topic/290245-unable-to-display-output-properly-using-php-echo-and-html-tags/#findComment-1486733 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.