Display search results in two rows - php

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

Related

Wrongly displayed table, row's aren't aligned to each other

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.

PHP Echo Table Column Alignment

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

How do I set a cell background colour based on its contents using PHP after reading values from MySQL?

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.

jquery highlight() breaking in dynamic table

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.

What's wrong with this php-generated table?

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

Categories