I have this issue, where I print out every detail of all users, but as you can see on the photo below, they aren't exactly aligned correctly. By that I mean that some firstnames' boxes are only let's say 50pixels wide, but some are wider.
Is there a way to fix this issue, and align each entry's details?
I attached the php code below, so you can have a look at it.
Thanks in advance
while($users=mysqli_fetch_assoc($getUsers)){
echo '<table border="1" cellpadding="1" cellspacing="1" width="80%" style="margin: 0 auto">';
echo '<tr class="allUsers">';
echo '<th>Username</th>';
echo '<th>First name</th>';
echo '<th>Last name</th>';
echo '<th>Email</th>';
echo '<th>Year group</th>';
echo '<th>Subject 1</th>';
echo '<th>Subject 2</th>';
echo "<th>Subject 1's teacher</th>";
echo "<th>Subject 2's teacher</th>";
echo '<th>Privilege</th>';
echo '<th>Own database</th>';
echo '</tr>';
echo '<tr>';
echo '<td>'.$users['username'].'</td>';
echo '<td>'.$users['first_name'].'</td>';
echo '<td>'.$users['last_name'].'</td>';
echo '<td>'.$users['email'].'</td>';
echo '<td>'.$users['year_group'].'</td>';
echo '<td>'.$users['subject'].'</td>';
echo '<td>'.$users['subject2'].'</td>';
echo '<td>'.$users['teacher'].'</td>';
echo '<td>'.$users['teacher2'].'</td>';
echo '<td>'.$users['is_admin'].'</td>';
echo '<td>'.$users['own_database'].'</td>';
echo '</tr>';
echo '</table>';
In addition to all of this, is there a way where I can just display the headers only once at the top, then just list all of the users' details?
You are creating a separate table for each user, when you actually need only one. Try separating the <table> tags and the headers from the loop, so they are only echoed once:
echo '<table border="1" cellpadding="1" cellspacing="1" width="80%" style="margin: 0 auto">';
echo '<tr class="allUsers">';
echo '<th>Username</th>';
echo '<th>First name</th>';
echo '<th>Last name</th>';
echo '<th>Email</th>';
echo '<th>Year group</th>';
echo '<th>Subject 1</th>';
echo '<th>Subject 2</th>';
echo "<th>Subject 1's teacher</th>";
echo "<th>Subject 2's teacher</th>";
echo '<th>Privilege</th>';
echo '<th>Own database</th>';
echo '</tr>';
while($users=mysqli_fetch_assoc($getUsers)){
echo '<tr>';
echo '<td>'.$users['username'].'</td>';
echo '<td>'.$users['first_name'].'</td>';
echo '<td>'.$users['last_name'].'</td>';
echo '<td>'.$users['email'].'</td>';
echo '<td>'.$users['year_group'].'</td>';
echo '<td>'.$users['subject'].'</td>';
echo '<td>'.$users['subject2'].'</td>';
echo '<td>'.$users['teacher'].'</td>';
echo '<td>'.$users['teacher2'].'</td>';
echo '<td>'.$users['is_admin'].'</td>';
echo '<td>'.$users['own_database'].'</td>';
echo '</tr>';
}
echo '</table>';
It looks like every 2 rows is its own table. Make the <tr>'s repeat, not the complete <table>'s.
Related
I have this code that works fine in other pages that I made but doesn't work properly on my summary page.
<?php
//AAFES-date1
$sqlAAFES1 = "SELECT * FROM aafes WHERE dueDate ='$date1'";
$qAAFES1 = $pdo->prepare($sqlAAFES1);
$qAAFES1->execute(array($date1));
$dataAAFES1 = $qAAFES1->fetch(PDO::FETCH_ASSOC);
if ($dataAAFES1){
echo '<table class="table table-condensed table-hover">';
echo '<tr>';
foreach ($pdo->query($sqlAAFES1) as $rowAAFES1){
echo '<td width="60%">'.$rowAAFES1['facilityName'].'</td>';
echo '<td style="text-align:right" width="40%">'.$rowAAFES1['totalQty'].'</td>';
echo '</tr>';
echo '</table>';
};
};
?>
as you can see the 2nd row doesn't align with the first row.
while on the other page, I used the same foreach code, but alignment is perfect. So I wanna ask what seems to be the problem with this one.
Don't close foreach loop after table. Put <tr></tr> inside foreach loop.
if ($dataAAFES1){
echo '<table class="table table-condensed table-hover">';
foreach ($pdo->query($sqlAAFES1) as $rowAAFES1){
echo '<tr>';
echo '<td width="60%">'.$rowAAFES1['facilityName'].'</td>';
echo '<td style="text-align:right" width="40%">'.$rowAAFES1['totalQty'].'</td>';
echo '</tr>';
};
echo '</table>';
};
I have this code reading values from a MySQL DB.
I've fiddled with classes in css to style the table cell backgrounds - but can't see how to do it in this case. It's almost like the php gets in the way - but I need it to read the results!!
The 'grades' in each variable will only ever be 1,2,3 or 4.
Any ideas?
<?php while($row = mysqli_fetch_array($result))
{
echo '<tr>';
echo '<td>'.$row['si_grade'].'</td>';
echo '<td>'.$row['att_grade'].'</td>';
echo '<td>'.$row['pro_grade'].'</td>';
echo '<td>'.$row['qot_grade'].'</td>';
echo '<td>'.$row['qoe_grade'].'</td>';
echo '</tr>';
}?>
Eventually, I want a 'set of coloured cells'...
You could set a CSS class based on the values and use that
.val1 { background-color: red; }
.val2 { background-color: blue; }
# etc
Then you do this in the PHP code
<?php while($row = mysqli_fetch_array($result))
{
echo '<tr>';
echo '<td class="val'.$row['si_grade'].'">'.$row['si_grade'].'</td>';
echo '<td class="val'.$row['att_grade'].'">'.$row['att_grade'].'</td>';
echo '<td class="val'.$row['pro_grade'].'">'.$row['pro_grade'].'</td>';
echo '<td class="val'.$row['qot_grade'].'">'.$row['qot_grade'].'</td>';
echo '<td class="val'.$row['qoe_grade'].'">'.$row['qoe_grade'].'</td>';
echo '</tr>';
}?>
The information from the database (1,2,3,4) will populate as data inside the cells of the table. What you want is to put it in the class of the <td> like such:
<?php while($row = mysqli_fetch_array($result))
{
echo '<tr>';
echo '<td class='.$row['si_grade'].'></td>';
echo '<td class='.$row['att_grade'].'></td>';
echo '<td class='.$row['pro_grade'].'></td>';
echo '<td class='.$row['qot_grade'].'></td>';
echo '<td class='.$row['qoe_grade'].'></td>';
echo '</tr>';
}
Edited to Add: Machavity's answer also gives the value in the cell, mine only colors the cell.
I am new to PHP; please help me.
I am trying to align two PHP tables with images in them side by side but they are displaying one below the other. I want two tables side by side under one heading and two tables side by side under second heading. I've seen some solutions in HTML but I am looking for PHP. Please find screenshot of my error and my code below:
Please feel free to ask for any clarifications.
$prodcatSQL="select prodcatid, prodcatname, prodcatimage from prodcat"; // create an $sql variable and store the sql statement
$exeprodcatSQL=mysql_query($prodcatSQL) or die (mysql_error());
while ($arrayprod=mysql_fetch_array($exeprodcatSQL))
{
echo "<strong>Using display: inline-block; </strong><br>\n";
echo "<table border=1 class=\"inlineTable\">\n";
echo "<tr>\n";
echo "<td><p><a href=products.php?u_prodcatid=".$arrayprod['prodcatid'].">";
echo $arrayprod['prodcatname'];
echo "<p><img src=images/".$arrayprod['prodcatimage']."></p>";
echo "</a></p></td>\n";
echo "</tr>\n";
echo "</table>\n";
}
echo "<h3><center>".$subheading."</center></h3>";
$treatcatSQL="select treatcatid, treatcatname, treatcatimage from treatcat"; // create an $sql variable and store the sql statement
$exetreatcatSQL=mysql_query($treatcatSQL) or die (mysql_error());
while ($arrayprod=mysql_fetch_array($exetreatcatSQL))
{
echo "<strong>Using display: inline-block; </strong><br>\n";
echo "<table border=1 class=\"inlineTable\">\n";
echo "<tr>\n";
echo "<td><p><a href=treatmentpackages.php?u_treatcatid=".$arrayprod['treatcatid'].">";
echo $arrayprod['treatcatname'];
echo "<p><img src=images/".$arrayprod['treatcatimage']."></p>";
echo "</a></p></td>\n";
echo "</tr>\n";
echo "</table>\n";
}
You have nested <p> tags which may be introducing unnecessary new lines. Remove the tags and replace them with separate <td> elements and the alignment should be fine.
$prodcatSQL="select prodcatid, prodcatname, prodcatimage from prodcat"; // create an $sql variable and store the sql statement
$exeprodcatSQL=mysql_query($prodcatSQL) or die (mysql_error());
while ($arrayprod=mysql_fetch_array($exeprodcatSQL))
{
echo "<strong>Using display: inline-block; </strong><br>\n";
echo "<table border=1 class=\"inlineTable\">\n";
echo "<tr>\n";
echo "<td><a href=products.php?u_prodcatid=".$arrayprod['prodcatid'].">";
echo $arrayprod['prodcatname'];
echo "</a></td><td><a href=products.php?u_prodcatid=".$arrayprod['prodcatid']."><img src=images/".$arrayprod['prodcatimage'].">";
echo "</td></a>\n";
echo "</tr>\n";
echo "</table>\n";
}
$treatcatSQL="select treatcatid, treatcatname, treatcatimage from treatcat"; // create an $sql variable and store the sql statement
$exetreatcatSQL=mysql_query($treatcatSQL) or die (mysql_error());
while ($arrayprod=mysql_fetch_array($exetreatcatSQL))
{
echo "<strong>Using display: inline-block; </strong><br>\n";
echo "<table border=1 class=\"inlineTable\">\n";
echo "<tr>\n";
echo "<td><a href=treatmentpackages.php?u_treatcatid=".$arrayprod['treatcatid'].">";
echo $arrayprod['treatcatname'];
echo "</a></td><td><a href=treatmentpackages.php?u_treatcatid=".$arrayprod['treatcatid'].">";
echo "<img src=images/".$arrayprod['treatcatimage']."></a>";
echo "</td>\n";
echo "</tr>\n";
echo "</table>\n";
}
If this does not work, please edit the HTML output into the question.
You need to add this to your table, if you don't want external CSS
Replace this in each,
echo "<table border=1 class=\"inlineTable\">\n";
With this
echo "<table border=1 class=\"inlineTable\" style=\"width:50%;float:left;\">\n";
I am trying to make a table via PHP, but when I load this, it displays it like this..
The code:
<table border="1" cellpadding="5">
<?php
while($test= mysql_fetch_assoc($countquery)){
echo '<tr><td>';
echo $test["count"];
echo 'x</td>';
};
while($row=mysql_fetch_array($topresult)) {
echo '<td width="150">';
echo $row["productnaam"];
echo '</td><td width="100" style="text-align:center;">€ ';
echo $row["prijs"];
echo '</td><td width="50" style="text-align:center;">';
echo '<a style="text-decoration:none;color:red;" href="#"><img width="25" src="trash.png"></a>';
echo '</td></tr>';
};
?>
</table>
My goal is to display a table of 4 columns by 3 rows..
EDIT:
Found it already, it makes a new <tr> tag everytime the first while is performed.
The block :
while($test= mysql_fetch_assoc($countquery)){
echo '<tr><td>';
echo $test["count"];
echo 'x</td>';
};
will create 3 cells 1x, 1x, 2x first,
next the block:
while($row=mysql_fetch_array($topresult)) {
echo '<td width="150">';
echo $row["productnaam"];
echo '</td><td width="100" style="text-align:center;">€ ';
echo $row["prijs"];
echo '</td><td width="50" style="text-align:center;">';
echo '<a style="text-decoration:none;color:red;" href="#"><img width="25" src="trash.png"></a>';
echo '</td></tr>';
};
will create cells from Monitor, so the result become like this.
To fix it, you should save the result from the first while loop to an array and go through at the 2nd while loop
You are echoing nested rows.
while($test= mysql_fetch_assoc($countquery)){
echo '<tr><td>';// Here you open a row
echo $test["count"];
echo 'x</td>';//No closing of row, you close the td and open another tr on the next iteration
};
I'm making a webpage and i add it a search engine. Here is the code from the results page.
$term = $_POST['term'];
$sql = mysql_query("select * from artists2 where Fname like '%$term%' or Genre like '%$term%' or Specialty like '%$term%' order by Fname");
while ($row = mysql_fetch_array($sql)){
echo '<table width="550" border="0" cellspacing="0" cellpadding="0">';
echo '<tr>';
echo '<td width="550" height="200"><img src="'.$row['Bio']. '" alt="" width="150" height="200"></td>';
echo '</tr>';
echo '<tr>';
echo '<td width="550" height="30">Name: '.$row['Fname'].'</td>';
echo '</tr>';
echo '<tr>';
echo '<td width="550" height="30">Genre: '.$row['Genre'].'</td>';
echo '</tr>';
echo '<tr>';
echo '<td width="550" height="30">Specialty: '.$row['Specialty'].'</td>';
echo '</tr>';
echo '<br/>';
echo '<br/>';
echo '<br/>';
echo '</table>';
}
?>
I want the results to be shown in two rows instead of one row which is shown now.
Also each result has a picture and 3 lines of text. I want the text to be show on the right side of the picture. Not below the picture as it shows now.
UPDATE 1
Dainis your code was very helpful. So close to what i need.
You code gives me this:
But i need something like, this:
Wow. That's some seriously outdated HTML code right there. You should never use tables for layout the way you're doing. There are much better ways of solving layouts like these nowadays.
Here's a JSFiddle which uses CSS for layout. The HTML in the fiddle is semantic meaning it accurately represents the data that you are displaying to your visitors (with or without sight) as well as search engines:
http://jsfiddle.net/CgbQH/
As you can see the list of search results is just that; a list. Each result has an image and a dl (some may argue dl isn't the right choice for key/value pairs though). CSS is then used to make the markup render the way you want.
Also, never ever stick user input (your $_POST['term']) directly into a query like that. Haven't you heard what happened to little Bobby Tables? http://xkcd.com/327/
Edit: I've used column-count to make the list render in two columns. column-count only works in modern browsers so if you have to support IE you can instead set each li to float: left; width: 50%. This will however render the items in a different order from column-count.
you need to remove some of your tags
while ($row = mysql_fetch_array($sql))
{
echo '<table width="550" border="0" cellspacing="0" cellpadding="0">';
echo '<tr>';
echo '<td width="550" height="200" valign="top"><img src="'.$row['Bio']. '" alt="" width="150" height="200"></td>';
echo '</tr>';
echo '<tr>';
echo '<td width="550" height="30" valign="top">';
echo 'Name: '.$row['Fname'].'<br />';
echo 'Genre: '.$row['Genre'].'<br />';
echo 'Specialty: '.$row['Specialty'];
echo '</td>';
echo '</tr>';
echo '</table>';
echo '<br/>';
echo '<br/>';
echo '<br/>';
}
How exactly do you want it to look like? Like this?
Then the code would be something like this:
while ($row = mysql_fetch_array($sql)){
echo '<table width="550" border="0" cellspacing="0" cellpadding="0" style="width: 40%; float: left;">';
echo '<tr>';
echo '<td width="550" height="200" align="left" valign="top"><img src="'.$row['Bio']. '" alt="" width="150" height="200"></td>';
echo '<td width="550" height="30" align="left" valign="top">';
echo '<p>Name: '.$row['Fname'].'</p>';
echo '<p>Genre: '.$row['Genre'].'</p>';
echo '<p>Specialty: '.$row['Specialty'].'</p>';
echo '</td>';
echo '</tr>';
echo '</table>';
}