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
Related
I need each row to have a unique button beside it which will delete the message. I need it to only be able to be deleted by the receiver. My first thought was to echo a HTML form inside the while loop. I don't think that would work though. If it does work what would the SQL statement inside if(isset()) look like? Each message has an ID so could I use that? If you need to see rest of the code let me know.
$Select = "
SELECT * FROM Messages
WHERE Receiver='$Identifier'
ORDER BY ID
DESC
LIMIT 10";
$Result = $Connect->query($Select);
if (mysqli_num_rows($Result) > 0) {
while ($Row = mysqli_fetch_assoc($Result)) {
echo nl2br("Sender = " . $Row["Sender"] . " Message = " . $Row["Message"] . "\n");
}
}
why don't try this:
echo "Sender = " . $Row["Sender"] . " Message = " . $Row["Message"] . "<button id=".$Row["ID"].">Delete</button>";
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
I'm building a food menu on a webpage. This menu includes drink items like Wine and Beer. Currently, they are categorized by Red or White and the type of wine (Pinot Grigio, Chardonnay, etc.). I have entered these wine items into a MySQL database with the following columns/values:
id(int), name(varchar), type(varchar), price_bottle(decimal), price_glass(decimal), red(bool)
My Current code:
$result = mysql_query("SELECT * FROM Wines WHERE type = 'Pinot Grigio' ");
while($row = mysql_fetch_array($result)) {
$name = $row['name'];
$type = $row['type'];
$price_bottle = $row['price_bottle'];
$price_glass = $row['price_glass'];
echo
"<div class='foodmenu-item'>
<div class='foodmenu-text'>
<h2>" . $type . "</h2>
<p>" . $name . "</p>
</div>
<div class='foodmenu-price'>
<p>$" . $price_bottle . " / " . $price_glass . "</p>
</div>
</div>";
}
Which is fine if I wanted to display each item out individually, but I want to Group by 'type' of wine (Pinot Grigio, Chardonnay, etc.) and then list each name of the wine under that 'type' so my CSS looks neat and organized.
For Example:
PINOT GRIGIO
Nobllissimo
Riff
CHARDONNAY
The Crusher
Bogle
Using the "GROUP BY" SQL syntax only displays the first result with my current code. All my attempts at making a foreach statement result in error, I'm not sure where to turn next.
How can I achieve this?
good manner is don't simply use "*", but list of columns
(reason: select only data which realy want to use - better performance, second if you later want add or change columns in the table then your life will be simplier ;-) )
I think you can try, select all from small table and only sort the data - type and name, then in while cycle do it group about type for your menu. Then you need only one query send to database and have all of it. Assumption is Wines is only small table, because select all consume a memory.
SELECT type, name, price_bottle, price_glass
FROM Wines
ORDER BY type, name;
Have you tries using 2 queries nested? Like this:
$result = mysql_query("SELECT DISTINCT(type) FROM Wines ORDER BY type");
while($row = mysql_fetch_array($result)) {
$type = $row['type'];
echo "<div class='foodmenu-item'>
<h2>" . $type . "</h2>";
$result2 = mysql_query("SELECT * FROM Wines WHERE type = '".type."' ");
while($row2 = mysql_fetch_array($result2)) {
$name = $row2['name'];
$price_bottle = $row2['price_bottle'];
$price_glass = $row2['price_glass'];
echo "
<div class='foodmenu-text'>
<p>" . $name . "</p>
</div>
<div class='foodmenu-price'>
<p>$" . $price_bottle . " / " . $price_glass . "</p>
</div>";
}
echo "</div>";
}
I am having an issue sorting my datatable by the column header.
I have a build-query mechanism on my site that can take in 1 or more values the user selects. The values get turned into PHP variables that then get passed into the query and prints out the datatable to the screen. The address bar URL is updated as well.
I want to be able to sort by the column headers of the table.
I will show you the PHP code bypassing the whole build-query function. I'll start right at the SORT portion and the query:
<?php
if ($_GET['sort'] == "") {
$sort_by = "BOL_NUMBER";
} else {
$sort_by = $_GET['sort'];
}
$select = "";
if ($_SESSION['where'] != "") {
$select = "SELECT * FROM `mainTable` WHERE (". $_SESSION['where'] . ") ORDER BY " . $sort_by . "";
}
// $_SESSION['where'] comes from the build query and can contain 1 or more values
$QueryResult = mysql_query($select) or die ();
$resnum = mysql_num_rows($QueryResult);
This next part is where the table gets populated:
if ($resnum == 0) {
echo "no results";
} else {
echo "<table>\n";
echo "<thead><tr>" .
"<th>BOL</th>" .
"<th>CONTAINER</th>" .
"<th>LOCATION</th>" .
"<th>STATUS</th>" .
"</tr></thead><tbody>\n";
while(($Row = mysql_fetch_assoc($QueryResult)) !== FALSE) {
echo "<tr>";
echo "<td>{$Row[BOL_NUMBER]}</td>";
echo "<td>{$Row[CONTAINER_NUMBER]}</td>";
echo "<td>{$Row[LOCATION_CITY]}</td>";
echo "<td>{$Row[STATUS_TYPE]}</td>";
echo "</tr>";
}
echo "</tbody></table>\n";
?>
There are many more columns. I just picked a couple.
I found this page for this next part:
Sorting html table with a href and php from sql database
So I tried to apply what I read in the link to the headers, like this:
"<th><a href='myPage.php?sort=BOL_NUMBER'>BOL</a></th>" .
"<th><a href='myPage.php?sort=CONTAINER_NUMBER'>CONTAINER</a></th>" .
"<th><a href='myPage.php?sort=LOCATION_CITY'>LOCATION</a></th>" .
"<th><a href='myPage.php?sort=STATUS_TYPE'>STATUS</a></th>" .
Now, I can click on the column headers, but when I do, it does not keep the user's selection and I can tell because the URL changes like this example below:
(this is just an example. it does not include the parameters in the table above)
(just keep note of the &sort in this url)
http://home.someCompany.com/myAPP/mypage.php?direction=I&type=&submit=Go&city=&pod=&terminal=&ramp=&container=&bol=&voyage=&conStatus=&con_location=&sort=&status=
Will change to this (if I select the header for CONTAINER):
http://home.someCompany.com/myAPP/mypage.php?&sort=CONTAINER_NUMBER
When this happens, the datatable is no longer on the screen. It's like it removes everything from the query and just adds the sort. But there is now nothing to sort.
The $_SERVER['QUERY_STRING'] superglobal variable give you access to the GET parameters. By using it you can keep the current parameters of the request.
So by changing your link by this :
$urlQuery = str_replace(array('&sort=BOL_NUMBER', '&sort=CONTAINER_NUMBER', '&sort=LOCATION_CITY', '&sort=STATUS_TYPE'), '', $_SERVER['QUERY_STRING']); // To clean previous sorting, maybe replace by a regex
"<th><a href='myPage.php?' . $urlQuery . '&sort=BOL_NUMBER'>BOL</a></th>" .
"<th><a href='myPage.php?' . $urlQuery . '&sort=CONTAINER_NUMBER'>CONTAINER</a></th>" .
"<th><a href='myPage.php?' . $urlQuery . '&sort=LOCATION_CITY'>LOCATION</a></th>" .
"<th><a href='myPage.php?' . $urlQuery . '&sort=STATUS_TYPE'>STATUS</a></th>" .
You should reach your goal.
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;
}