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
};
Related
Right now I have this program that echoes out an image and a text ten times. It does so all in one column. I want to make it so that it fills up one whole page by going horizontally after it hits the end of the page in one column. After all the columns fill up, then it can go to the next page. Here is my code:
while($row = mysqli_fetch_array($result)) {
echo '<div>';
echo "<p style='font-size:1.05em;color:#0e3c68;font-weight:bold;'>" . $row['question'] . "</p>";
<img src="data:image/jpeg;base64,'.$row['imagename'] .'" height="125" width="125" class="img-thumnail" />
echo '<div>';
Just your code as following.
echo '<div style="text-align: center;display: inline-block;">';
echo "<p style='font-size:1.05em;color:#0e3c68;font-weight:bold;'>" . $row['question'] . "</p>";
<img src="data:image/jpeg;base64,'.$row['imagename'] .'" height="125" width="125" class="img-thumnail" />
echo '<div>';
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.
I have some code which takes my results from the database and then wraps them in to a div, so that each of the results are situated in to individual boxes.
Whilst the results are fine, the problem i am having is that the boxes are repeating inside one another and not formating in to individual boxes which list below one another.
below is my php code
$sVisit_Id='';
while ($row = mysql_fetch_array ($result)) {
$url1 = $row['url1'];
if ($row['visit_id']!=$sVisit_Id)
{
echo '</table>';
?> <div class="box1095"><?php
echo '<table><tr><td class="test" width="350px;"><strong>'.$referrer.'</strong><br /></td><td>'.$search_term.'</td></tr><table>';
$sVisit_Id=$row['visit_id'];
echo '<tr bgcolor="#333" height=30px"><td width="200px">Date/Time</td><td width="750px">Webpage</td><td width="150px"></td></tr>';
}
echo '<tr class="active" bgcolor="" onMouseOver="this.bgColor="gold";" onMouseOut="this.bg"#222222";"><td width="200px" border="">'.$row['timedate'].
'</td><td width="750px" border="">'. $row['url1'].'</td><td width="15px" align="right" border="">'. "<a href=". $url1 ." class=''> " ?><img src="images/go_button.fw.png" width="100" height="30" alt="GO" target="_blank"/><?php "</a>" .'</td></tr>';
} ?></div><?php
echo '</table>';
The image below shows a screenshot of the results.
Any suggestions or guidance would be appreciated.
Thanks
You should try something like this
echo '<table>';
while ($row = mysql_fetch_array ($result)) {
// everything goes here
echo '<tr><td>Webpage</td><td></td></tr>';
echo '<tr><td>some text</td><td>some text</td><td><img src="" /></td></tr>';
}
echo '</table>';
Keep table out of the loop (both, starting and closing tags), so, there will be one table with multiple rows (tr).
Update : You have (<table>...<table>) between if condition, as given below
echo '<table><tr><td class="test" width="350px;"><strong>'.$referrer.'</strong><br /></td><td>'.$search_term.'</td></tr><table>';
There is no closing tag, instead you have <table> so, your table is never closed. Also, there is only echo '</table>'; at the beginning of if condition, without starting tag (within loop) but closing tag is outside of loop.
Update : Also you have following (pointed out by David) at the end of your last line within the loop, (not echoed/printed).
<?php "</a>" .'</td></tr>';
I've got a little issue with a highlight function I'm working on. I basically load records out of a database that match the current form data in certain ways. Then, when someone is filling in the form, if they are describing an issue that already exists in my system, it will highlight words that their description has in common with the existing record(s). My issue is that the table breaks. It will work to a certain extent, but sometimes it breaks the PHP loop portion out of the rest of the table, and it then has no formatting, and the highlighting function will not work. To be more specific, once broken, the td tags in the body of the table do not follow the formatting of the header row.
Conditions that cause the undesirable effect:
tabbing through the text area
If too many classes have to be removed or applied at once (via deleting all, adding many words or deleting or searching for a single character with many occurrences)
html on the main page && script to trigger the highlighting
<textarea name="description" id="description"></textarea>
<script>
var delay = (function(){
var timer = 0;
return function(callback, ms){
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
})();
$(function(){
$("#description").keydown(function(){
delay((function(){
$("#displayer *").removeClass('highlight');
var1 = $('textarea#description').val().split(' ');
for (var i=0;i<var1.length;i++){
$("#displayer *").highlight(var1[i], "highlight")};
}),1000);
});
});
</script>
the external php that builds the searched table based on an ajax call is this:
echo '<TABLE BORDER="0" CELLSPACING="5" CELLPADDING="5" id="displayer"><FONT FACE="ARIAL">';
echo ' <tr> ';
echo ' <td width="20" ALIGN="LEFT" height="1">ID</td>';
echo ' <td width="89" ALIGN="LEFT" height="1">Date</td> ';
echo ' <td width="200" ALIGN="LEFT" height="1" >Description</td>';
echo ' <td width="89" ALIGN="LEFT" height="1" >Solution</td>';
echo ' <td width="20" ALIGN="LEFT" height="1" >User:</td>';
echo ' <td ALIGN="LEFT" height="1" >Key?:</td>';
echo ' <td ALIGN="LEFT" height="1" >Part:</td>';
echo ' <td ALIGN="LEFT" height="1" >Classified As:</td>';
echo ' </tr> ';
for ($i=1; $i <= $num_results; $i++)
{
$row = mysql_fetch_array($result1);
echo '<TR BGCOLOR="#EFEFEF">';
echo '<TD width="20">';
echo stripslashes($row['0']) ;
echo '</TD>';
echo '<TD width="89" >';
echo $row['1'] ;
echo '</TD>';
echo '<TD width="200">';
echo stripslashes($row['6']) ;
echo '</TD>';
echo '<TD width="89">';
echo stripslashes($row['11']) ;
echo '</TD>';
echo '<TD width="20">';
echo $row['5'] ;
echo '</TD>';
echo '<TD>';
if ($row['8'] == 1)
{echo 'Yes' ;}
else
{echo 'No' ;}
echo '</TD>';
echo '<td>'.$row['10'].'</td>';
echo '<td>'.$row['9'].'</td>';
echo '</TR>';
}
echo '</TABLE>';
external highlight plugin:
jQuery.fn.highlight = function (str, className) {
var regex = new RegExp(str, "gi");
return this.each(function () {
$(this).contents().filter(function() {
return this.nodeType == 3 && regex.test(this.nodeValue);
}).replaceWith(function() {
return (this.nodeValue || "").replace(regex, function(match) {
return "<span class=\"" + className + "\">" + match + "</span>";
});
});
});
};
I think I should add a test for empty with some kind of escape, to solve the first condition, but with the second, I'm not sure what's happening. Any suggestions are definitely appreciated. Sorry for the post being huge and convoluted, but I wanted everyone to have all the information I could provide.
$(function(){
$("#description").keydown(function(){
delay((function(){
var divClone = $("#disp_hidden .serial_display").clone();
$("#displayer .serial_display").replaceWith(divClone);
if ($.trim($('textarea#description').val()) != ''){
var1 = $('textarea#description').val().trim().split(' ');
for (var i=0;i<var1.length;i++){
$("#displayer *").highlight(var1[i], "highlight")};
};
}),1000);
});
});
hidden table clone, replaces at new edit, fixed.
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>';
}