Jump to content

Unable to display output properly using php "echo" and html tags


Go to solution Solved by Ch0cu3r,

Recommended Posts

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;
					}
				}
			}	
?>

post-172415-0-38396200-1407063651_thumb.png

post-172415-0-12132700-1407063663_thumb.png

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>";

  • Solution

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 by Ch0cu3r

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.