I am trying to achieve a sorted list with a section separator from a mysql table via php, but am having a bit of trouble. Instead of putting my soup in the soup header like I want, I am putting it in the following header(like I don't want)!
Here is the visual output:
2-Soup
3-Salad
2 Soup Demo: Tom KaKai
4-Entree
3 Salad Demo: Spinach Salad
4 Entree Demo: Pork Chop
4 Entree Demo: Spicy Topped Shrimp
here is my code:
$cat = null;
while($row = mysql_fetch_array($result))
{
if( $row['catnum'] != $cat )
{
echo '<h1>' . $row['catnum'] . '-' . $row['ctgry'] . '</h1>';
$cat = $row['catnum'];
}
echo "<table><tr><td>" . $row['catnum'] . " </td><td>" . $row['ctgry'] . "</td><td> " . $row['Shrt_Desc'] . "</td></tr>";
}
You're not closing your <table> tag and are starting a new table for every DB row retrieved, so structurally your HTML page is a total mess.
You'd want something like this:
$first = true;
while ($row = mysql_fetch_array($result)) {
if ($row['catnum'] != $cat) {
if (!$first) {
echo '</table>'; // close the table if we're NOT the first row being output.
}
$first = false; // no longer the first table, so disable this check.
echo '<h1> etc...';
echo '<table>'; // start new table
$cat = $row['catnum'];
}
echo '<tr><td>' etc.... // output a table row
}
This code will only output the <table> and <h1> stuff when you change categories.
Related
I want to display 9 different tables from my sql database in 9 different html created tables on the website.
In detail: I have 9 tables ("dt_bookmarks_01", "dt_bookmarks_02" etc.) with 4 columns "id" (which is primary and auto increment), icon (for favicon), link (url) and text (for the display text).
I've created 9 different html tables with bootstrap and want to output the content of each table in a different bootstrap table of my site.
My problem is that i have no idea how to get different "foreaches" or counter for each different table.
To automaticaly add new rows to the bootstrap table I use the count and foreach function. problem here is: I dont know how to seperate them from each other. If i have 4 entries in sql table 1 it multiplies the one and only entrie of sql table 2 to match the current count of 4.
I am very new to sql and php so I guess I just miss some fundamental functions or something.
document header:
php
$sql = "
SELECT *
FROM dt_bookmarks_01, dt_bookmarks_02";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
$conn->close();
and for the html table I use:
php
<tbody>
<!--begin: SQL Selection -->
<?PHP
$count = 0;
foreach($rows as $item){
if (!empty($item['icon'])) {
$icon = '<img src="assets/media/bm-icons/'. $item['icon'] . '">';
}else{
$icon = '<img src="assets/media/bm-icons/default.png">';
}
$count++;
echo "<tr>";
/*echo "<td>" . $count . "</td>";*/
echo "<td> " . $icon . "</td>";
echo "<td> <a href=\"" . $item['link'] . "\"'>" . $item['text'] . "</a> </td>";
echo "<td></i> ";
echo "</i></td>";
echo "</tr>";
}
?>
<!--end: SQL Selection -->
</tbody>
I do not have a database on hand to give you an answer with complete code, but here is the idea:
<?php
for ($i = 1; $i <= 9; $i++)
{
$query = "SELECT index1,index2 FROM dt_bookmarks_0$i";
echo "<h1>This is the content of table $i</h1>";
# RUN THE QUERY HERE !!!
echo "<table>";
# EXTRACT THE RESULTS
foreach $rows as $item
{
echo "<tr><td>$item[index1]</td><td>$item[index2]</td></tr>"
}
echo "</table>";
echo "<br><br>";
}
?>
Loop on your tables.
In each table loop, you output the HTML code to display it's content.
Avoid SELECT *, specify your indexes (research "sql why avoid SELECT *")
So you loop twice. One time to go through the tables, the other to loop on the results.
so here is the new working code.
header:
<?PHP
require_once('/htdocs/_nt/mysql/data.php');
$sql = "
SELECT *
FROM dt_bookmarks";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
$conn->close();
?>
and for the table output:
<?PHP
$count = 0;
foreach($rows as $item){
if ($item['category'] == talk) {
$count++;
echo "<tr>";
echo "<td> " . $icontalk . "</td>";
echo "<td> <a href=\"" . $item['url'] . "\"'>" . $item['text'] . "</a> </td>";
echo "</tr>";
}else{
echo "";
}
}
?>
Please forgive me as I am beginner in PHP.
I am trying to populate 2 two column table with inventory information. With my current code, I have the below image:
I would like to have two stores next to each other in this table. i.e Store Number 2 and Store Number 7 on the table row in the table, then Store 10 and 11 on the same row, and so on. Below is the code I am using so far to achieve this:
global $wpdb;
$result = $wpdb->get_results(
$wpdb->prepare( "
SELECT STORE_NAME,REPLACE(STORE_NAME, ' ', '-') as STOREURL, INVENTORY, STORE_NUMBER FROM StoreInventory
WHERE SKU = %s",
$product_sku
)
);
if ($result){
echo '<table class=\'inventory\'>';
foreach($result as $row) {
echo '<tr><td><div><a href=\'https://mystore.com/stores/'. $row->STOREURL . '\' target=\'_parent\'>Store Number: ' . $row->STORE_NUMBER . '</a><br/>' .$row->INVENTORY. ' on hand. </div></td></tr>';
}
echo '</table>';
} else {
echo '<table class=\'nostock\'><td>This item is out of stock, check back later for updated information!</td></table>';
}
The problem I am having is that I am looping through the record set row by row, and adding the data to each row. I have attempted to have a loop inside a loop, which will not give me the correct results.
Is it possible to split the result set into two multi-dimensional arrays and loop through each one separately then add them to the table? Can I call out a specific row with a counter within the loop?
Any advise or direction would be a great help.
A relatively simple approach would be to create a dummy variable to store the column. EX:
$column_number = 0;
echo '<table class=\'inventory\'><tr>';
foreach($result as $row) {
echo '<td><div><a href=\'https://mystore.com/stores/'. $row->STOREURL . '\' target=\'_parent\'>Store Number: ' . $row->STORE_NUMBER . '</a><br/>' .$row->INVENTORY. ' on hand. </div></td>';
$column_number += 1;
if ($column_number == 2) {
echo '</tr><tr>';
$column_number = 0;
}
}
echo '</tr></table>';
Final answer based on ARubiksCube's answer:
if ($result){
$column_number = 0;
echo '<table class=\'inventory\'><tr>';
foreach($result as $row) {
echo '<td \' width=\'50%\' ><div><a href=\'https://shopliquornl.com/stores/'. $row->STOREURL . '\' target=\'_parent\'>' . $row->STORE_NAME . '</a><br/>' .$row->INVENTORY. ' on hand</div></td>';
$column_number += 1;
if ($column_number == 2) {
echo '</tr><tr>';
$column_number = 0;
}
}
echo '</table>';
} else {
echo '<table class=\'nostock\'><td>This item is out of stock, check back later for updated information!</td></table>';
}
I'm trying to pull data from 1 column of a db and making it into a link where the URL is being pulled from another column. Unfortunately, I have not been able to do this. I've attached the screenshot of the output here's the code I'm using:
$sql = "SELECT DISTINCT item_title, item_url, item_date, item_author FROM nbth";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<a>" . $row["item_title"]. " url: " . $row["item_url"]. " <br> - By " . $row["item_author"] . " " . $row["item_date"]. "<br><br></a>" ;
$HTML .= "".$row['item_title']."";
echo $HTML;
}
} else {
echo "0 results";
}
Screenshot of the results:
[![enter image description here][1]][1]
The titles do become links but the get looped again and again, the first title in the first line, then next the first title and second, then the first, second and third, so on and so forth. So, how do I fix this looping and still display the titles as links.
$HTML .= "".$row['item_title']."";
Remove the period (string concat) operator before the equal sign:
$HTML = "".$row['item_title']."";
It causes each iteration of the loop to concat the "".$row['item_title'].""; part every time to the $HTML variable.
Also, pls enclose the href attribute's value by quotation marks(").
echo $html outside the loop
or
while($row = $result->fetch_assoc()) {
$HTML ='';
echo "<a>" . $row["item_title"]. " url: " . $row["item_url"]. " <br> - By " . $row["item_author"] . " " . $row["item_date"]. "<br><br></a>" ;
$HTML = "".$row['item_title']."";
echo $HTML;
}
} else {
echo "0 results";
}
I'm executing this type of code inside a table.
while($row = mysql_fetch_array($result))
{
echo "" . $row['id'] . "";
echo "" . $row['name'] . "";
echo "" . $row['car'] . "";
}
Which works great and gives me a numbered list (1 Mike Volvo, 2 Mike Ford) for example. The problem is that I now have multiple users in the same table so there are gaps in the list where someone else's entry is since i'm using auto incremented ID for numbers.
So assuming I have 5 entries I obviously want it to be 1-5 but right now its 5-30-45-65 or whatever.
Anyway I looked around for a solution and I found $number = 1; $number++; to be effective and it kind of works and gives me a 1-5 list independent of whatever the ID's are.
The problem is that when I reverse the list using ORDER BY xxx desc the last entry becomes 1 and the first entry becomes 5. But I always want the first entry to remain 1 and the last entry to be 5.
Do you have any ideas on how to create this function using PHP?
You need to simple user $number as you mentioned as in the following code
$number = 1;
while($row = mysql_fetch_array($result))
{
echo $number;
echo "" . $row['name'] . "";
echo "" . $row['car'] . "";
++$number;
}
Even if in your SQL you have order by it does not matter in that case.
From what I understand of your question... You are on the right track using the following code...
$LastId ='';
$number = 0;
while($row = mysql_fetch_array($result))
{
if($LastId != $row['id']){
$number++;
}
echo $number;
echo "" . $LastId = $row['id'] . "";
echo "" . $row['name'] . "";
echo "" . $row['car'] . "";
}
So...
If the 'id' isn't the same as the last 'id' (not the same user) it will increment it by 1. Otherwise it will stay the same.
Hope this is what you looking for and helps.
Although this is pretty trivial in php, I would not use php for it. You can easily do this in html:
echo "<ol>";
while($row = mysql_fetch_array($result))
{
echo "<li>" . $row['name'] . $row['car'] . "</li>";
}
echo "</ol>";
What I'd like is to create a new unordered list for each new value in a mysql result column.
My query looks like this and i'm throwing it into a data array:
$connection = dbconnect();
$getusers = "SELECT users.usr_id, users.usr_firstname, users.usr_lastname, user_groups.group_name FROM users INNER JOIN user_groups ON users.usr_group = user_groups.group_id ORDER BY group_name ASC, users.usr_firstname ASC, users.usr_lastname ASC";
$result = mysql_query($getusers);
$data_array = array();
while($data = mysql_fetch_array($result)){
$data_array[] = $data;
}
Now I need to display that data so that each new user_group is an unordered list, and each row with that same group, comes up as a list item of that unordered list.
it's very similar to this question ( PHP/MySQL Query Results ), but i'm having trouble getting the closing ul's in the right place. Here's my code for outputting, though I know it's wrong because the li's aren't really children of the ul's.
$previousgroup = "";
foreach($data_array as $users){
if($users['group_name'] != $previousgroup){
echo "<ul class=\"group\">" . $users['group_name'] . "</ul>";
}
else{
echo "<li id=\"m" . $users['usr_id'] . "\"><h4>" . $users['usr_firstname'] . " " . $users['usr_lastname'] . ", " . $users['cred_name'] . "</h4></li>";
}
}
Is there a more efficient way of doing this? Thanks for your help.
You could change the while loop this way:
$data_array = array();
while($data = mysql_fetch_array($result)){
$data_array[$data['group_name']][] = $data;
}
and you would get an array that contains your group_name's as index of other arrays which contain your data. To create the list, you could do the following:
<ul>
<?php
foreach ($data_array as $temp_key => $temp_array)
{
?>
<li>
<?php echo $temp_key; ?>
<ul>
<?php
foreach ($temp_array as $datum)
{
echo ("<li>" . $datum['usr_firstname'] . "</li>";
}
?>
</ul>
</li>
<?php
}
?>
</ul>
Hope it works for you
if($users['group_name'] != $previousgroup){
echo "</ul><ul class=\"group\"><li>" . $users['group_name'] . "</li>";
}
echo "<li id=\"m" . $users['usr_id'] . "\"><h4>" . $users['usr_firstname'] . " " . $users['usr_lastname'] . ", " . $users['cred_name'] . "</h4></li>";
The code in your loop should look like this, and you need to open and close a <ul> from outside the loop.
echo '<ul>';
foreach () { // here is the loop }
echo '</ul>';
THis is doing it the "dirty" way with printing out as you go, at least.
The way I would do it would be to build a 2D array indexed first by group_name and then by user ID. It would mean making two passes at the data as a second loop would be needed to display the data, but generally speaking you're not going to feel the difference.