<?php
include 'preCode.php';
include 'header.php';
echo '<body><div class="standardLayout">';
include 'systemMenu.php';
echo '<h4>All Charges</h4>';
$user = unserialize($_SESSION['user']);
$query = "SELECT * FROM billingItems WHERE userID= ' ".$user->userID. " ' ORDER BY deliveryTimestamp DESC";
$result = mysqli_query($db, $query);
while($row = mysqli_fetch_array($result))
{
echo '<p>';
echo '<a href="billingHistory1.php?deliveryTimestamp=' .$row["deliveryTimestamp"]. '">'.
' Order Delivered on' . '</a>' .$row['deliveryTimestamp'] ;
}
echo '</div></body></html>';
$_SESSION['user'] = serialize($user);
include 'footer.html';
?>
output of above file:
All Charges
Order Delivered on2015-05-06 13:26:50
Order Delivered on2015-05-06 13:26:50
Order Delivered on2015-05-06 13:26:50
Order Delivered on2015-05-03 22:11:23
Order Delivered on2015-05-03 22:11:23
Order Delivered on2015-05-03 22:11:23
Order Delivered on2015-05-03 22:11:23
If you will see the output then you will notice that first three links are same and last four are same.
I need to reduce multiple links to one.
Want to display only two links one for each.
To do that, i need to use a if statement inside the while loop. If deliveredTimestamp is same don't display it otherwise display it. Please help.
Why not try adding unique deliveryTimestamps to an array and then rendering your links from those?
$timestamps = array();
while($row = mysqli_fetch_array($result)){
if ( !in_array($row['deliveryTimestamp'], $timestamps)) {
array_push($timestamps, $row['deliveryTimestamp']);
}
}
foreach ($timestamps as $timestamp) {
echo '<p>';
echo '<a href="billingHistory1.php?deliveryTimestamp='.$timestamp.'">'.
' Order Delivered on' . '</a>' .$timestamp ;
echo '</p>';
}
You can use group by to get the number of records reduced
Use this query
$query = "SELECT * FROM billingItems
WHERE userID= ' ".$user->userID. " '
GROUP BY deliveryTimestamp
ORDER BY deliveryTimestamp DESC";
Related
Need to showup the Qty of "IDs" or "Class" from a Site in my header.
It a job-site, i want to count the IDs or class of my Job Listings and show the number in my header (Menu).
How is it possible? With "getElementsByTagName" it works like a charme. Tried with "H3" Tags, but i have to many h3 on the site, so the qty / number is not correct.
my code now:
<?php
$htmlString = file_get_contents('https://www.mywebsite.de/unternehmen/jobs/');
//Create a new DOMDocument object.
$htmlDom = new DOMDocument;
//Load the HTML string into our DOMDocument object.
#$htmlDom->loadHTML($htmlString);
//Extract all h3 elements / tags from the HTML.
$h3Tags = $htmlDom->getElementsByTagName('h3');
$jobanzahl = $htmlDom->getElementById('jobtitel')->nodeValue;
echo "Total H3 Tags: ". count($h3Tags)."<br/>";
It would be much easier to use the SELECT COUNT with your database, because you can manage which job offers are active and which are not. You can also ORDER BY starting date if you wish !
Here is the dataset I used for this example :
Each job has a unique id (int) as a primary key, a title (varchar), a description (text), a starting date (date) and a status (active or not - boolean). You can archive inactive offers for monthly or yearly reports, but you must count only active ones.
At first, I display the offers on a very basic html page :
$sql = "SELECT * FROM job";
$stmt = $conn->prepare($sql);
$stmt->execute();
$result = $stmt->get_result();
echo "<h1>JOBS</h1>";
echo "<br>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<h2>" . $row['title'] . "</h2>";
echo "<p>" . $row['description'] . "</p>";
echo "<p><small>Starting on : " . $row['start_date'] . "</small></p>";
echo "<hr>";
}
}
And then, I count and display the number of active offers :
$sql = "SELECT COUNT(*) AS c FROM job WHERE isActive = 1";
$stmt = $conn->prepare($sql);
$stmt->execute();
$result = $stmt->get_result();
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<p>There are " . $row['c'] . " offers</p>";
}
Here is the final result :
Finally, you should look at this codepen to circle the result :
https://codepen.io/jnbruno/pen/vNpPpW
include "connection file";
$query = "SELECT * FROM Blog";
$result = mysqli_query($query);
$num_results = mysqli_num_rows($result);
for($i=0; $i<$num_results; $i++) {
$row = mysqli_fetch_assoc($result);
echo "<div class="blogEntry"><h4>" . $row['title'] . "</h4><h5>" . $row['date'] . "</h5><p>"
. $row['text'] . "</p></div>";
}
Hi, so, I'm trying to loop through all my blog posts and display them to the page in order so the newest entry is first but I can't seem to get it too work. tried a few different ways and always white screen! Any help would really be appreciated :). I'm also going to add a search function and filters, I don't need this now but any suggestions where to look to find info about implementing these would also be really helpful. Link to where I'm putting this code on my website: http://www.obeytoplay.com/. Thanks!
tried a few different ways and always white screen!
That's because there's a syntax error in your code. Either escape the inner double quotes(") using backslash(\) or use single quotes(').
Method(1):
include "connection file";
$query = "SELECT * FROM Blog";
$result = mysqli_query($query);
$num_results = mysqli_num_rows($result);
for($i=0; $i<$num_results; $i++) {
$row = mysqli_fetch_assoc($result);
echo "<div class=\"blogEntry\"><h4>" . $row['title'] . "</h4><h5>" . $row['date'] . "</h5><p>" . $row['text'] . "</p></div>";
}
Method(2):
include "connection file";
$query = "SELECT * FROM Blog";
$result = mysqli_query($query);
$num_results = mysqli_num_rows($result);
for($i=0; $i<$num_results; $i++) {
$row = mysqli_fetch_assoc($result);
echo "<div class='blogEntry'><h4>" . $row['title'] . "</h4><h5>" . $row['date'] . "</h5><p>" . $row['text'] . "</p></div>";
}
I'm trying to loop through all my blog posts and display them to the page in order so the newest entry is first
Use ORDER BY in conjunction with SELECT clause to reorder the result set, like this:
SELECT * FROM Blog ORDER BY column_name DESC/ASC;
Here's the reference:
ORDER BY
everyone. I'm having a bit of a PHP conundrum here and I couldn't find a good answer that already existed. You see, I'm working on a project where I have to take a classmate's discography website and revamp it with PHP, to where, instead of having the album covers and tracklists hard-coded in, it would query the database for them. My problem is that I have to keep the general style of his site intact, and I'm having trouble doing that. Basically his styles depend on having the album cover, name, and tracklists in div tags, and the style he's got in place is achieved through both Bootstrap and his own, custom CSS stylesheet.
Before I start to ramble, my question is: is there any way to wrap looping output in HTML tags? I need to get the album cover, album name, and tracklists in a div tag, but only the tracklists loop. Here is the code I have in place to query the database:
<?php
require ('mysqli_connect.php');
// Connect to database server
mysql_connect("localhost", "admin", "instructor") or die(mysql_error());
// Select database
mysql_select_db("phprediscography") or die(mysql_error());
// SQL query
$q = "SELECT DISTINCT albums.albumname, albums.albumID, albums.coverart
FROM albums
JOIN tracks
ON albums.albumID=tracks.albumID"; //select UNIQUE results from database
$t = "SELECT trackname FROM tracks WHERE albumID = 1";
$b = "SELECT trackname FROM tracks WHERE albumID = 2";
$n = "SELECT trackname FROM tracks WHERE albumID = 3";
$r = "SELECT trackname FROM tracks WHERE albumID = 4";
$result = mysqli_query($dbcon, $q);
$result1 = mysqli_query($dbcon, $t);
$result2 = mysqli_query($dbcon, $b);
$result3 = mysqli_query($dbcon, $n);
$result4 = mysqli_query($dbcon, $r);
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { //loop through database to get each album
echo '<img class="img-responsive" src=' . $row['coverart'] . '>' . '<br />';
echo '<h2>' . $row['albumname'] . "</h2><br />";
if ($row['albumID'] == 1) {
foreach($result1 as $row1) { //loop through tracks and output to page
echo '<p>' . $row1['trackname'] . '</p>';
}
}
if ($row['albumID'] == 2) {
foreach($result2 as $row2) { //loop through tracks and output to page
echo '<p>' . $row2['trackname'] . '</p>';
}
}
if ($row['albumID'] == 3) {
foreach($result3 as $row3) { //loop through tracks and output to page
echo '<p>' . $row3['trackname'] . '</p>';
}
}
if ($row['albumID'] == 4) {
foreach($result4 as $row4) { //loop through tracks and output to page
echo '<p>' . $row4['trackname'] . '</p>';
}
}
}
// Close the database connection
mysql_close();
?>
If I need to post anything else, let me know, this is my first-ever question so I'm just kind of feeling it out.
By doing your $t = "SELECT trackname FROM tracks WHERE albumID = #"; and if($row['albumID']==#) you are essentially still hardcoding similar to your friend. Just do 1 query, where you join all the tracks. Then when looping, group by the albumname -
<?php
require('mysqli_connect.php');
// SQL query
$q = "SELECT albums.albumname, albums.albumID, albums.coverart, tracks.trackname
FROM albums
JOIN tracks
ON albums.albumID=tracks.albumID";
$result = mysqli_query($dbcon, $q);
$current_albumID = ""; //create current albumID var to be used below.
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){//loop through database to get each album
if($row['albumID'] != $current_albumID){
echo '<img class="img-responsive" src='.$row['coverart'] . '>' . '<br />';
echo '<h2>' . $row['albumname'] . "</h2><br />";
$current_albumID = $row['albumID']; // set current albumID to this albumID
}
echo '<p>' . $row['trackname'] . '</p>';
}
?>
Try something like this instead: Get all the data you're after in your first query, then use php to process that into your output:
$q = "SELECT albums.albumname, albums.albumID, albums.coverart, tracks.trackname
FROM albums
JOIN tracks ON albums.albumID=tracks.albumID";
$result = mysqli_query($dbcon, $q);
$lastAlbumId = null;
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
if ($lastAlbumId != $row['albumID']) {
echo '<img class="img-responsive" src="'.htmlentities($row['coverart']).'"><br />';
echo '<h2>'.htmlentities($row['albumname']).'</h2><br />';
}
echo '<p>'.htmlentities($trackname).'</p>';
$lastAlbumId = $row['albumID'];
}
A few things to note:
I added use of htmlentities to escape the user data so malicious people can't type in HTML somewhere and have it appear on your site. This is something you should do almost everywhere you're displaying data from a database in a html site, except for very rare cases where you know what you're doing.
You probably don't need those <br /> tags - <h2> is a block level element so it'll force itself onto it's own line anyway (unless there's some silly CSS rules somewhere).
Also note - the above code is untested - typed straight into browser. There may be some syntax errors - let me know if you see any problem and I'll happily edit the answer. (or you can suggest an edit).
So on my blog page I am using substr to display only a bit of the articles. The idea is once someone clicks one of the headings they will be sent to a page displaying the full article.
When i click the link i get the right heading in the url for example i click the 4th blog entry i will be redirected to index.php?id=4.The problem is that on this page the content remains the same, nothing changes. How do I get it so when i click the article I will be directed to a page containing only that article in full and no others. Thanks in advance guys this has been wrecking my head all day.
<?php
$dbinfo = "SELECT blog_id, title, date, body FROM content ORDER BY blog_id DESC LIMIT 0, 3";
$result = mysql_query($dbinfo) or die(mysql_error());
$return = '<p> Go Back To Content Page</p>';
if(mysql_num_rows($result) !=0):
while($row = mysql_fetch_assoc($result)){
echo '<div id="roundedbox"><h2>' . $row['title'] . ' </h2>';
echo '<div id="date"><h5><p>' . $row['date'] . '</p></h5></div>';
echo substr('<p>' . $row['body'] . '</p>',0, 90)." .... "." read more</div>";
}
else:
echo '<p> UH OOH! THERE IS NO SUCH PAGE IT DOES\'T EXIST </p>';
echo $return;
endif;
?>
The query you have in your code will return all the records for the table, what you want is the query to return the record based on the id parameter from the $_GET['id']. It's a good idea to make sure you're only using integers to prevent sql injection.
From your code you need to have a separate query to fetch the appropriate record using the $_GET['id'] parameter as follows...
//the parameter is set and it is an integer
if(isset($_GET['id']) && is_int($_GET['id'])) {
$blogId = (int)$_GET['id'];
$query = "SELECT blog_id, title, date, body FROM content WHERE blog_id='$blogId'";
// run query and get record data and output it
} else {
//code to return all records as list
$dbinfo = "SELECT blog_id, title, date, body FROM content ORDER BY blog_id DESC LIMIT 0, 3";
$result = mysql_query($dbinfo) or die(mysql_error());
$return = '<p> Go Back To Content Page</p>';
if(mysql_num_rows($result) !=0):
while($row = mysql_fetch_assoc($result)){
echo '<div id="roundedbox"><h2><a href="index.php?id=' . $row['blog_id'].$row['title'] . ' </a></h2>';
echo '<div id="date"><h5><p>' . $row['date'] . '</p></h5></div>';
echo substr('<p>' . $row['body'] . '</p>',0, 90)." .... "." read more</div>";
}
else:
echo '<p> UH OOH! THERE IS NO SUCH PAGE IT DOES\'T EXIST </p>';
echo $return;
endif;
}
While dynamically generating a page with different types of contents , the "post" content is appearing above the static content which is being generated.I want it the other way around. Does there appear to be anything in my code that would make this happen, or do you think the problem has something to do with my database? Thanks.
$query = "SELECT * FROM content WHERE pages LIKE '%$pageID%'";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
// Display pages's static content
if ($row['type'] == "static") {
echo "
<h2>" . $row['tile'] . "</h2>
<content>" . $row['body'] . "</content>
";
}
// Display pages's posts
else {
echo "
<h2>" . $row['tile'] . "</h2>
<content>" . $row['body'] . "</content>
";
}
SELECT * FROM content WHERE pages LIKE '%$pageID%' ORDER BY type desc
Add this to the end of your query:
ORDER BY CASE WHEN type = 'static' THEN 0 ELSE 1 END