0

With the following code in PHP, I want to echo the data fetched from some MySQL tables:

<html>

<head>
<meta http-equiv="Content-Language" content="en-gb">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Search Results</title>
</head>

<body bgcolor="#EFEFEF">
<table border="0" align=center width="95%" id="table1" bgcolor="#FFFFFF">
    <tr>
        <td>
        <img border="0" src="index_files/text827854609.gif" width="260" height="67"></td>
    </tr>
    <tr>
        <td><hr color="#C0C0C0" size="4"></td>
    </tr>
    <tr>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td>
        <p align="left"><font face="Verdana" size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
        Search Student Name For Results</font></td>
    </tr>
    <tr>
        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td>
    </tr>
    <tr>
        <td>
        <form method="POST" name="Informationform" action="searchresults.php">
            <!--webbot bot="SaveResults" U-File="fpweb:///_private/form_results.csv" S-Format="TEXT/CSV" S-Label-Fields="TRUE" B-Reverse-Chronology="FALSE" S-Builtin-Fields -->
            <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <b><font face="Verdana" size="2">Enter Name in Full</font></b> :&nbsp;
            <input type="text" name="fullname" size="50">
            <input type="submit" value="Search" name="senda"></p>
        </form>
        <?php
        if(isset($_POST['senda'])){
            include 'mysqlconn.php';
            $con = mysqli_connect($host, $dbuser, $pass, $db) or die('Cannot Connect');
            $name = mysql_escape_string($con,$_POST['fullname']);

            $sql = "SELECT * FROM scores WHERE fullname = '$name'";
            $result = mysqli_query($con,$sql)  or die("Error: ".mysqli_error($con));
            while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
            {
                echo $tablez;
            }mysqli_free_result($result);
        }
        ?>
        </td>
    </tr>
    <tr>
        <td>
    $tablez = "<table border=0 width=99% id=table2>
            <tr>
                <td width=299 align=center><b>
                <font face=Verdana size=2 color=#808080>Full Name</font></b></td>
                <td width=171 align=center><b>
                <font face=Verdana size=2 color=#808080>studentNo</font></b></td>
                <td width=276 align=center><b>
                <font face=Verdana size=2 color=#808080>SubjectName</font></b></td>
                <td width=106 align=center><b>
                <font face=Verdana size=2 color=#808080>GPA</font></b></td>
                <td width=176 align=center><b>
                <font face=Verdana size=2 color=#808080>CGPA</font></b></td>
                <td align=center><b>
                <font face=Verdana size=2 color=#808080>SCORE</font></b></td>
            </tr>
            <tr>
                <td width=299 align=center> . $row['Fullname'] . </td>
                <td width=171 align=center>. $row['studentNo']. </td>
                <td width=276 align=center>. $row['SubjectName'].</td>
                <td width=106 align=center> .$row['GPA']. </td>
                <td width=176 align=center>.$row['CGPA']. </td>
                <td align=center> .$row['SCORE'].</td>
            </tr>
        </br></table>";
        </td>
    </tr>
    <tr>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td><hr color="#808080" size="1">
        <p>
        <font face="arial" class="ws7" style="font-size: 9.3px; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(212, 212, 212);">
        EM Software Limited Nigeria<span class="Apple-converted-space">&nbsp;</span></font><font class="ws7" style="font-size: 9.3px; color: rgb(0, 0, 0); font-family: arial; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(212, 212, 212)">©</font><font face="arial" class="ws7" style="font-size: 9.3px; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(212, 212, 212);"><span class="Apple-converted-space">&nbsp;</span>2016 
        All rights Reserved.</font></td>
    </tr>
</table>
</body>

</html>

However, it doesn't work as I expect.

This is the output I'm getting:

enter image description here

How can I echo the data from the database?

4
  • 2
    the variable $tablez should be declared inside the loop, not after Commented Feb 13, 2016 at 10:06
  • RamRaider, its Inside the loop, look closely Commented Feb 13, 2016 at 10:08
  • no, you try to echo the variable inside the loop but the actual variable is defined after. Commented Feb 13, 2016 at 10:11
  • @NnaerichDoughan I think you should loop through the variable row only. The header Full Name, Studentno etc.. shouldn't be inside the while loop. What you say? Commented Feb 13, 2016 at 10:11

2 Answers 2

2

The variable $tablez should be defined inside the loop as it gets content from the recordset. As it was declared after the loop most likely you would have gotten some sort of error

To avoid the repetition of the table header, define /echo outside the loop and only repeat the tablerows inside the loop.

Rather than using inline styles it would be far easier to maintain the code if you used a stylesheet, that way you could remove all of the styling from the various html elements and govern the look and layout with one exteernal file.

$tableheader="
    <table border='0' width='99%' id='table2'>
        <tr>
                <td width='299px' align='center'><b>
                <font face='Verdana' size='2' color='#808080'>Full Name</font></b></td>
                <td width='171px' align='center'><b>
                <font face='Verdana' size='2' color='#808080'>studentNo</font></b></td>
                <td width='276px' align='center'><b>
                <font face='Verdana' size='2 'color='#808080'>SubjectName</font></b></td>
                <td width='106px' align='center'><b>
                <font face='Verdana' size='2' color='#808080'>GPA</font></b></td>
                <td width='176px' align='center'><b>
                <font face='Verdana' size='2' color='#808080'>CGPA</font></b></td>
                <td align='center'><b>
                <font face='Verdana' size='2' color='#808080'>SCORE</font></b></td>
        </tr>";
$tablefooter='</table>';


echo $tableheader;

while( $row = mysqli_fetch_array( $result, MYSQLI_ASSOC ) ){

    $tablerows = "
            <tr>
                <td width='299px' align='center'>" . $row['Fullname'] . "</td>
                <td width='171px' align='center'>". $row['studentNo']. "</td>
                <td width='276px' align='center'>". $row['SubjectName']."</td>
                <td width='106px' align='center'>" .$row['GPA']. "</td>
                <td width='176px' align='center'>".$row['CGPA']. "</td>
                <td align='center'>" .$row['SCORE']."</td>
            </tr>";
    /* echo the table row */
    echo $tablerows;
}

echo $tablefooter;/* close the table */
mysqli_free_result( $result );
Sign up to request clarification or add additional context in comments.

2 Comments

it does look wrong... that's the trouble with coding in notepad - thanks for the headsup
One more thing bro.. Use header<tr> outside the loop
0

You need to print HTML result inside the while loop as:

$tablez_col = "
<table border=0 width=99% id=table2> <tr> 

<td width=299 align=center><b> <font face=Verdana size=2 color=#808080>Full Name</font></b></td> 

<td width=171 align=center><b> <font face=Verdana size=2 color=#808080>studentNo</font></b></td> 

<td width=276 align=center><b> <font face=Verdana size=2 color=#808080>SubjectName</font></b></td> 

<td width=106 align=center><b> <font face=Verdana size=2 color=#808080>GPA</font></b></td> 

<td width=176 align=center><b> <font face=Verdana size=2 color=#808080>CGPA</font></b></td> 

<td align=center><b> <font face=Verdana size=2 color=#808080>SCORE</font></b>
</td> 
</tr>";

echo $tablez_col; // print columns

// your loop start
while( $row = mysqli_fetch_array( $result, MYSQLI_ASSOC ) )
{
echo "
<tr> 
<td width=299 align=center>" . $row['Fullname'] . "</td> 

<td width=171 align=center>". $row['studentNo']. "</td> 

<td width=276 align=center>". $row['SubjectName']."</td> 

<td width=106 align=center>" .$row['GPA']. "</td> 

<td width=176 align=center>".$row['CGPA']. "</td> 

<td align=center>" .$row['SCORE']."</td> </tr> 
</table>"; 
} // loop end

5 Comments

Now it keeps repeating itself , i mean the header over and over again like this Full Name studentNo SubjectName GPA CGPA SCORE Jon Elwin 200451517 Mathematics 4.00 4.32 92 Full Name studentNo SubjectName GPA CGPA SCORE Jon Elwin 200451517 English 4.02 4.82 96 Full Name studentNo SubjectName GPA CGPA SCORE Jon Elwin 200451517 French 3.85 3.92 81 Isnt there anything to do to make the header repeat just once?
@nnaerich-doughan use echo $tablez; outside the loop
Still like before . I want it to have a header, like the header and the image above, should have a header and then print the data @devpro
@nnaerich-doughan header will print once and than all data right?
yes @devpro, header print once and then data Follows

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.