Creating grid rather than a table - php

For the table below, rather than paginate, I would like to create a grid of results.
So I would like to echo out a set of 10 results, then continue the process 200 pixels to the right. Then after 10 more results, continue 200 pixels to the right of that.
How can I do this?
$query2 = "SELECT street1, city, state, zip, phone, website
FROM addresses
WHERE submissionid=$submissionid
ORDER BY street1 DESC";
$result2 = mysql_query($query2);
$arr2 = array();
echo "<table class=\"commentecho3\">";
while ($row2 = mysql_fetch_array($result2)) {
echo '<tr>';
echo '<td>'.strtoupper($row2["street1"]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td>'.strtoupper($row2["city"]).', '.strtoupper($row2["state"]).' '.strtoupper($row2["zip"]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td>'.strtoupper($row2["phone"]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td>'.strtoupper($row2["website"]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td></td>';
echo '</tr>';
echo '<tr>';
echo '<td></td>';
echo '</tr>';
}
echo "</table>";

Is it really necessary to use tables there? Like Webgal suggested, could be really easy done using floated divs:
Create a class in css
.foo{float:left;padding-right:200px;}
Update PHP (this can be done different ways):
$i=0;
echo('<div class="foo">');
while ($row2 = mysql_fetch_array($result2)) {
echo '<p>'.strtoupper($row2["street1"]).'</p>';
.................................
$i++;
if ($i=10){
echo('</div><div class="foo">');
}
}
echo('</div>');

Related

Missing the first row of a SELECT statment

I have a database table called 'Modules' and I am trying to select all of the rows from that table and display them in a table. Each row has a column called MOrder which ranges from 1 - how ever many modules are available.
Here is my code:
$sql_query = "SELECT * FROM Modules WHERE CourseID = ". $CourseID ." ORDER BY MOrder ASC";
$result = mysqli_query($dbconfig, $sql_query);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$count = mysqli_num_rows($result);
echo '<div class="row">';
echo '<div class="col-md-12">';
echo '<table class="table">';
if($count > 0) {
$_SESSION['countMOrder'] = $count;
echo '<tr>';
echo '<th>Module Title</th> ';
echo '<th></th>';
echo '</tr>';
while ($row = mysqli_fetch_array($result)) {
echo '<tr>';
echo '<td>'. $row['Title'] .'</td> ';
echo '<td>Take Module</td>';
echo '</tr>';
}
}
echo '</table>';
echo '</div>';
echo '</div>';
?>
However, for whatever reason the statement is missing out the module with MOrder 1 and always starting with 2?
Why is this?
You are calling $row = mysqli_fetch_array($result, MYSQLI_ASSOC); in the third line of your pasted code, which is pulling the first array from the results.
This is then being overwritten in your while loop:
while ($row = mysqli_fetch_array($result)) { // <-- overwritten here with item 2
//...
}
Because in the 3rd line of your code you call
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
once, and then in the loop you start calling the
$row = mysqli_fetch_array($result)
again, thus overwriting the $row variable with the 2nd row. Get rid of the 1st $row = mysqli_fetch_array($result) line.
Try this code.
$sql_query = "SELECT * FROM Modules WHERE CourseID = ". $CourseID ." ORDER BY MOrder ASC";
$result = mysqli_query($dbconfig, $sql_query);
$count = mysqli_num_rows($result);
echo '<div class="row">';
echo '<div class="col-md-12">';
echo '<table class="table">';
if($count > 0) {
$_SESSION['countMOrder'] = $count;
echo '<tr>';
echo '<th>Module Title</th> ';
echo '<th></th>';
echo '</tr>';
while ($row = mysqli_fetch_array($result)) { //for every fetch we'll get one row.
echo '<tr>';
echo '<td>'. $row['Title'] .'</td> ';
echo '<td>Take Module</td>';
echo '</tr>';
}
}
echo '</table>';
echo '</div>';
echo '</div>';
At the beginning, you fetch the first row.
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
In while loop, fetch function returns second row.

Fetch complex table data from database

I have a table in database and I want to fetch the data from it. I know how to fetch data from database using php. But the problem is that
this table is different in structure. For example
and so on...
As you can see this table is like a pivot table. That is the real problem for me. All the entries of a single person should be in single row. But it is in different rows and single column (all entries of single person is in value column).
I have tried to fetch table with regular way like this
<?php
/*
Template Name: Registration
*/
get_header();
$results = $wpdb->get_results( "SELECT * FROM wp_cf_form_entry_values", ARRAY_A );
$count = $results->num_rows;
if(!empty($results)) {
echo "<table width='100%' border='1' cellspacing='1'>";
echo "<tbody>";
foreach($results as $row){
echo "<tr>";
echo "<td><center>" . $row['value'] . "</center></td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
} else {
echo "<table width='100%' border='1' cellspacing='1'><tr><td>No Records Found</td></tr></table>";
}
get_footer();?>
But with the above code it's showing all the results in a single column and that's what I expect from above code.
So I don't know the correct code to represent this table as single row entries of single person. I searched a lot but didn't get much.
I'm pretty sure you looking for something like this:
horizontal representation: (by Person)
<?php
/*
Template Name: Registration
*/
get_header();
global $wpdb;
//Create a mulit dimensional array for each person.
$persons = array();
$fields = $wpdb->get_results( 'SELECT * FROM wp_cf_form_entry_values');
foreach($fields as $field){
$persons[$field->entry_id][$field->slug] = $field->value;
}
//the table
echo '<table width="100%" border="1" cellspacing="1">';
$i=0;
foreach($persons as $id=>$values){
//the table header (only in the first loop)
if($i==0){
echo '<tr>';
foreach($values as $key=>$val){
echo '<th>'.$key.'</th>';
}
echo '</tr>';
}
// one line per person
echo '<tr id="'.$id.'">';
foreach($values as $key=>$val){
echo '<td>'.$val.'</td>';
}
echo '</tr>';
$i++;
}
if($i==0){
echo "<table width='100%' border='1' cellspacing='1'><tr><td>No Records Found</td></tr></table>";
}
echo '</table>';
get_footer();?>
vertical representation (by field slug)
<?php
/*
Template Name: Registration
*/
get_header();
global $wpdb;
//Create a mulit dimensional array for each slug.
$field_slugs = array();
$fields = $wpdb->get_results( 'SELECT * FROM wp_cf_form_entry_values');
foreach($fields as $field){
$field_slugs[$field->slug][$field->entry_id] = $field->value;
}
//the table
echo '<table width="100%" border="1" cellspacing="1">';
$i=0;
foreach($field_slugs as $slug=>$values){
//the table header (only in the first loop)
if($i==0){
echo '<tr>';
foreach($values as $person_id=>$val){
echo '<th></th>';
echo '<th>Person '.$person_id.'</th>';
}
echo '</tr>';
}
// one line per person
echo '<tr id="'.$slug.'">';
echo '<td><b>'.$slug.'</b></td>';
foreach($values as $person_id=>$val){
echo '<td>'.$val.'</td>';
}
echo '</tr>';
$i++;
}
if($i==0){
echo "<table width='100%' border='1' cellspacing='1'><tr><td>No Records Found</td></tr></table>";
}
echo '</table>';
get_footer();?>
You use foreign key entries_id so you can retrieve from that table ie
$results=$wpdb->get_results("select * from entries_table");
foreach($results as $result){
$entries_id=$result->id;
$entry_detail=$wpdb->get_results(SELECT * from form_field_table where entries_id='$entries_id' ");
foreach($entry_detail as $ent_detail){?>
<td><?php echo $ent_detial->slug;?></td><td><?php echo $ent_detial->value;?></td>
<?php }
}

I need some assistance with using a specific php function within an HTML webpage

I am currently working on a PHP and SQLite application. It does have HTML for the webpages being created. I am currently using PDO to connect the database to my online server. When I get to the webpage that allows me to type in what I am searching for then it will display what I have found in the echo statements below. I want to be able to have just the item name, that acts as a hyperlink; when it is clicked on it will go to another webpage (I believe) that will display the item's name, amount, and a short description. Is there a way to use PHP for this action or should I go with the HTML tagging?
if($_POST && isset($_POST['search'])) {
echo "<br>\n";
$query = $mysql->prepare('SELECT * FROM Items WHERE Name = :partname');
$subst = array ('partname' => $_POST['search']);
$query->execute($subst);
echo "<TABLE>";
echo "<tr>";
echo "<td>Name</td>";
echo "<td>Amount</td>";
echo "<td>Detail</td>";
echo "</tr>";
while ($row = $query->fetch()) {
//print_r($row);
echo "<tr>";
echo "<td>$row[Name]</td>";
echo "<td>$row[Amount]</td>";
echo "<td>$row[Detail]</td>";
echo "</tr>";
}
echo "</TABLE>";
} else echo "Item searched for was not found.";
from what i understand
if($_POST && isset($_POST['search'])) {
echo "<br>\n";
$query = $mysql->prepare('SELECT * FROM Items WHERE Name = :partname');
$subst = array ('partname' => $_POST['search']);
$query->execute($subst);
echo "<TABLE>";
echo "<tr>";
echo "<td>Name</td>";
echo "</tr>";
while ($row = $query->fetch()) {
//print_r($row);
$name = $row['Name'];
echo "<tr>";
echo "<td><a href='details.php?name={$name}'>{$name}</a></td>";
echo "</tr>";
}
echo "</TABLE>";
} else echo "Item searched for was not found.";
To use Associative Arrays within double quotes, you need to use curly braces:
echo "<td>{$row['Name']}</td><td>{$row['Amount']}</td><td>{$row['Detail']}</td>";

looping through table rows php CSS

I am looping through an array of results that I have gathered from the database. There are no problems in regards to displaying the data gained from the database. The problem sits within the styling of the data.
I have some CSS for the code below, it styles the first row of data but the rest are just echoed with no styling although they do look as if they sit within the table that is being defined within the php.
$user = $_SESSION['sess_uid'];
$conn = new mysqli(localhost, root, DBPASS, DBNAME);
$sql = "SELECT * FROM reports WHERE userID = '" . $conn->real_escape_string($user)."';";
// Performs the $sql query on the server
$result=mysqli_query($conn,$sql);
$rows[] = array();
echo '<table>';
echo '<thead>';
echo '<tr>';
echo '<th> Report Name </th>';
echo '<th> Category Name </th>';
echo '<th> Sub Category Name </th>';
echo '<th> Date Uploaded </th>';
echo '</tr>';
echo '</thead>';
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo '<tbody>';
$rows[] = $row;
echo '<tr>';
echo '<td>'.$row['reportName'].'</td>'.'<td>'.$row['categoryName'].'</td>'.'<td>'.$row['subcategoryName'].'</td>'.'<td>'.$row['reportDateUploaded'].'</td>';
echo '</tr>';
echo '</tbody>';
echo '</table>';
};
Any thoughts?
Thanks
You need to make your while statement only contain your tr and td elements.
You need to put the tbody and table tags out of your while loop.
echo '<tbody>';
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$rows[] = $row;
echo '<tr>';
echo '<td>'.$row['reportName'].'</td>'.'<td>'.$row['categoryName'].'</td>'.'<td>'.$row['subcategoryName'].'</td>'.'<td>'.$row['reportDateUploaded'].'</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';

SQL Query only retrieving one row

I have a a php website with some code on it to pull from a database after the user has defined some search terms and then show them a table with all their information in it
The problem is even when i do a select * from the tables, i am only getting the first row back.
Code:
$result = mysql_query("SELECT trees.*
FROM trees
INNER JOIN price
ON trees.ID=price.treeid
");
$num_rows = mysql_num_rows($result);
if($num_rows == 0) {
echo "No rows retrieved";
} else {
echo $num_rows;
}
i have 2 rows in my database:
Spruce El Sorbeous Sprucious Green Green Green 100 200
its a tree ma! true NULL NULL NULL
Mayday el daymay red green white 10 4000000
GOING DOWN true Grey true false
when i print out the $num_rows up there, it is only one.
When i print out my table below, there is only one row:
echo '<table border = 0 cellpadding=0 >';
echo '<tr>';
echo '<td><b><u>Name</b></u></td>
<td><b><u>Latin Name</b></u></td>
<td><b><u>Primary Color</b></u></td>
<td><b><u>Secondary Color</b></u></td>
<td><b><u>Fall Color</b></u></td>
<td><b><u>Trunk Color</b></u></td>';
echo '<td><b><u>Description</b></u></td>
<td><b><u>Height</b></u></td>
<td><b><u>Spread</b></u></td>
<td><b><u>Drought Resistant?</b></u></td>
<td><b><u>Flowering?</b></u></td>
<td><b><u>Fruit Producing?</b></u></td>';
echo '</tr>';
while ($row = mysql_fetch_assoc($result)) {
echo '<tr>';
echo '<td>';
echo $row['name'];
echo '</td>';
echo '<td>';
echo $row['latinname'];
echo '</td>';
echo '<td>';
echo $row['primarycolor'];
echo '</td>';
echo '<td>';
echo $row['secondarycolor'];
echo '</td>';
echo '<td>';
echo $row['fallcolor'];
echo '</td>';
echo '<td>';
echo $row['trunkcolor'];
echo '</td>';
echo '<td>';
echo $row['description'];
echo '</td>';
echo '<td>';
echo $row['height'];
echo '</td>';
echo '<td>';
echo $row['spread'];
echo '</td>';
echo '<td>';
echo $row['droughtresistant'];
echo '</td>';
echo '<td>';
echo $row['flowering'];
echo '</td>';
echo '<td>';
echo $row['fruitproducing'];
echo '</td>';
echo '</tr>';
}
echo '<table>';
INNER JOIN will only return values that have NOT NULL values in both tables. You are probably joining stuff with a NULL value. Use a LEFT or a RIGHT join instead!
Since there's no example of the data in the PRICE table, I'm guessing that only one row was joined.
The question remains, why are you doing that JOIN? You're not collecting any data from the PRICE table, so what's the point.
BTW, where in the data is the ID?

Categories