Display each row from MySQL in a separate DIV - php

I have a posts table which contains a field for the posts. Let's say I have 10 posts that i want to show in 10 divs. How should I proceed in doing that? I've managed to get the full contents using a while loop, but that only shows the full contents in one place, and I want to have individual divs (so i can use different background colors) for each individual post.
Help me out please, I hope it makes sense. Just think at the way facebook displays posts for example. Each post has it's own box. I want something similar.
Snippet of the code I have to get the posts is available here:
<?php
require_once("includes/database.php"); // Get the database connection
$get_post = "SELECT full_post FROM posts";
$show_post = mysqli_query($connection, $get_post);
if (!$show_post) {
echo "Could not load post. " . "(" . mysqli_error($connection) . ")";
}
while ($post = mysqli_fetch_assoc($show_post)) {
echo $post["full_post"] . "<br />";
}
mysqli_free_result($show_post);
?>

The easiest method of doing this is creating the divs from the while function that's showing your posts and adding CSS classes to them.
Example:
while ($post = mysqli_fetch_assoc($show_post)) {
echo '<div class="blue">';
echo $post["full_post"] . "<br />";
echo '</div>';
}

I imagine you are looping through the individual posts using the while-loop. You can start a new div every round.
while($post = mysqli_fetch_assoc($result)) {
print '<div>';
print $post->content;
print '</div>';
}

Related

PHP/MySQL - how to complete dropdown selection from list

I'm cobbling together a dropdown list that I would like to display the '[ve_venue]", "[ve_town]' from a MySQL query and, upon selection set a variable that I can use to pass the ve_ID on to an update query that adds a venue ID number to a separate table as a lookup key.
So far I've got some code that I've pieced together from various places and I can get it to display the town in a dropdown - I just need to add the venue field to the dropdown so I get "venue, town" in the list and I also need to be able to pass the ve_ID to a variable, say, $ve_ID so I can call it in some separate code (that will be on the same page in a separate include).
Here's what I've got so far....
<?
include('login_admin_sucess.php');
// Connects to your Database
include '../connect_include.php';
$query = "SELECT ve_ID, ve_venue, ve_town FROM `co_venues` ORDER BY ve_ID ";
$result = mysql_query($query);
while($r=mysql_fetch_array($result))
{
$ve_venue = $r['ve_venue'];
$result_array[$ve_venue][] = $r['ve_town'];
}
echo "<select name='x'>";
foreach($result_array as $v_venue => $value)
{
foreach($value as $title) {
echo "<option value='" . $v_venue . "'>" . $title . "</option>";
}
}
echo "</select>";
?>
I realise that mysql_ is deprecated...I'll work on that once I've got everything functioning.
You can concat the id and venue. For example:
$ve_venue = $r['ve_venue'] . "|" . $r['ve_ID'];
When you use the variable make a split with charapter "|" with preg_split function.

Having problems with my post system

I know the basics of MySQL and PHP. I've set up a posting system that echoes the fields from my table.
The only problem is, since this is a news page, the newest posts need to be on top. And when the page echoes back the results, it puts the first entry I made (in the table) at the top.
This is my code:
// Selecting The Table
$result = mysql_query("SELECT * FROM posts")
or die(mysql_error());
// Connecting To The Rows, And Echoing Back The Results
while($row = mysql_fetch_array($result)) {
echo "<div class='post'>";
echo "Title: " . $row['title'];
echo "<br />";
echo $row['content'];
echo "</div>";
I am not sure how to make the latest post the first to show up.
I still want it to echo back the older entries though.
I can use phpMyAdmin if necessary.
You need to order your results.
SELECT * FROM posts ORDER BY date_posted DESC

Get the name of the link in the where clause of php query when I click the link?

I have made a website in which the users can select a value from a dropdown menu and get some information from a database. I used ajax to send the request to the database (so the page doesn't get refreshed when I send the request). Here is the part of the jquery function:
$.ajax({
type:'POST',
url:'activities_code.php',
data: {datastr:datastr, datastr1:datastr1},
success:function(response){
$("#msg").html(response);
}});}); // there are other functions before..
The results appear on the main container of the webpage. They are composed of a title and some text. I echo the title in such a way so it is a link. I also give to each element an id and a class so I can call it later. Here is the corresponding code:
echo "<table id=\"container\">";
$num_results = 0;
while ($row=mysqli_fetch_array($result, MYSQLI_ASSOC)) {
// Here the columns of title and information are printed
echo "<tr><td>";
echo "".$row['title']."";
echo "<br>";
echo $row['PK'];
echo "</td></tr>";
echo "<tr><td>";
echo $row['Information'];
echo "</td></tr>";
}
What I am trying to do now is: When I click on the title (which is a link), a new page to open in which a php script runs a query and show more information:
Here is what I have:
<?php
include('connect.php');
$query = "SELECT title,Information from activities where title='?????'";
$result = mysqli_query($dbcon, $query) or die('no available data');
echo "<table>";
$num_results = 0;
while ($row=mysqli_fetch_array($result, MYSQLI_ASSOC)) {
// Here the columns of title and information are printed
echo "<tr><td>";
echo "".$row['title']." ";
echo "</td></tr>";
echo "<tr><td>";
echo $row['Information'];
echo "</td></tr>";
// Here I sum up the number of the results
$num_results=$num_results+1;
}
?>
I am trying to find a way to put in my query, in the where clause, the name of the title that I selected:
$query = "SELECT title,Information from activities where title='?????'";
Any help would be much appreciated. Let me know if everything is clear or I didn't explain some point clearly.
Thanks.
D.
You can get the title using $_GET global variable and URL parameter. Try changing this line:
echo "".$row['title']."";
to
echo "".$row['title']."";
then you can get the title with these code:
$title = $_GET['title'];
make sure you sanitize the value first. I hope this will help you.
link:
PHP $_GET

How to style PHP output?

I am learning php and made a simple application to save peoples names to a database then pull all the saved names into a list lower down on the page.
this is the code that is getting the names: echo "<div class=\"results\"> $row[Name] </div>";
I put a div with the class of results so that I could style it. At this point all I wanted to do was center the results. After styling it with css to be centered, looking at the page source each name that was echo'ed has a div around it with the class of results. They are all centered like I wanted which is good but this does not seem like the best way to do it because there will be a new div created for every new name in the database (right now there are 18 divs. Way too many!).I was hoping only one div would be created but I was obviously wrong about that.
So, is it possible to pre-create a div in the html markup with nothing in and echo the names into the div? Or do you think there would be a way to make it all output at once so it was all within the one div?
Here is the rest of the code so you get an idea of whats happening:
<?php
$connect = mysql_connect("localhost","user","0123");
if(!$connect){
die("Failed to connect: " . mysql_error());
}
if(!mysql_select_db("Test")) {
die("Failed to select database: " . mysql_error());
}
$results = mysql_query("SELECT * FROM Friends");
while($row = mysql_fetch_array($results)){
echo "<div class=\"results\"> $row[Name] </div>";
}
if ($_POST[name] !="") { $sql="INSERT INTO Friends
(name) VALUES('$_POST[name]')";
if (!mysql_query($sql,$connect))
{
die('Error: ' . mysql_error());
}
echo "<div id=\"added\"> $_POST[name] added to the database </div>";
}
else die("")
?>
Since you output the div inside your while loop, you will get a new div for each result. Just move the div outside the loop:
echo "<div class=\"results\">";
while($row = mysql_fetch_array($results)){
echo $row['Name'];
}
echo "</div>";
Note that you should always quote your keys. Use $row['Name'] instead of $row[Name] (obviously not if Name is a defined constant).
i think it's better to have some variable to store the output:
$out .= "<div class=\"results\">";
while($row = mysql_fetch_array($results)){
$out .= $row[Name]."<br />";
}
$out .= "</div>";
echo $out;
Don't let php output html tags. It looks bad in code and your editor will not recognise it as html and make a big mess in colouring it. Generally I would advise to do all your php processing first and then make the HTML with pieces of php output in it. Something like this:
Note that I replaced your div with an ul, as your list of persons seems better suited as an ul than a div. Obviously you can use any tag you'd like in the HTML
<?php
// do all php stuff here
$persons = /* get collection from database here */
?>
<html>
<head>
</head>
<body>
<ul id="ListOfPersons">
<? foreach ($persons as $person): ?>
<li>
<?= $person["Name"] ?>
</li>
<? endforeach ?>
</ul>
</body>
</html>
You can separate the div creation from the results echoing :
echo '<div class="results">';
while($row = mysql_fetch_array($results)){
echo $row['Name'].'<br/>';
}
echo '</div>';
and if you don't want the div if there are no results (no friends) you can use mysql_num_rows

PHP click text in a list, go to page with just clicked text

I have a table in a database that i'm showing all tweets in a page, so how would i go about clicking a single tweet on the page and going to a new page that just shows that one tweet.
Some sort of GET through the href?
This is my PHP code:
while ($row = mysql_fetch_array($result)){
echo "<p>" . $row['tweet'] . "</p><br />";
}
Make each tweet a link pointing to some page and pass the id of that tweet to the page:
while ($row = mysql_fetch_array($result)){
echo "<p><a href='singlepage.php?id=" . $row['tweet_id']. "'>" . $row['tweet'] . "</a></p><br />";
}
Then on singlepage.php, you'd basically have the same code as the page listing all the tweets, except that it would include the sql "where tweed_id = " . $_GET['id'], so you'd only get that one result.
Be sure to validate your input so that if someone tweaks the URL and changes the ID, then they can't insert malicious sql commands.

Categories