How to display comments inline - php

So I have this code (which works) that prints out the comments a user has just written. The issue im having is with displaying the comments. The username is displayed above the comment and I just want them to be displayed next to each other for example
'Username:'comment''
I have tried putting display:inline; in my CSS for h4 and p but it messes up everything else that i have put into these elements.
Does anyone know the answer?
This is my code
//Print out existing comment
$query = "SELECT * FROM comments JOIN users ON comments.userID = users.ID WHERE salonID=$salonid";
$result = mysqli_query($db_server, $query);
if (!$result) die("Database access failed: " . mysqli_error($db_server));
while ($row = mysqli_fetch_array($result)){
$str_comments .= "<div id='comments'><h4>" . $row['Username'] ."</h4><p>'" . $row['comment'] . "'</p>";
$str_comments .="<img src='" . $row['imagename'] ."' /></div>";
}

You put username inside <h4></h4> which is a block element. It's the same for <p></p>.
Block element will take a full row so you won't be able to place username & comment in a line.
You already give display:inline style for <h4></h4> & <p></p> it should be fine.
I encourage you to use a <span></span> instead which is an inline element by default.
Focus on this:
while ($row = mysqli_fetch_array($result)){
$str_comments .= "<div id='comments'>" .
"<span style='font-weigth:bold;'>" . $row['Username'] ."</span>" .
"<span>'" . $row['comment'] . "'</span>" .
"<img src='" . $row['imagename'] ."' />".
"</div>";
}

The easiest way to do it on your website is use annote ( http://annote.in ). A javascript based inline comment plugin. You can get started on any platform in minutes.

Related

Changing the output of my HTML form

I am trying to create a form using data from MySQL and my current output is not looking as expected so I just need some simple changes to my current form
My current output in the browser: http://gyazo.com/f4668ca59586ec0d4d1500ea6f7b6257
What I am looking to do is
fix the problem with the first column called 'Long Jump A' so its
next to a drop down menu, and you can see the bottom one is next to
nothing.
Secondly, How can I swap the order of the drop down and the
event? So the event is on the left then the drop down is on the
right?
Moving the form down from the top so there is a space
The code:
<?php
require_once 'db/connect.php';
//Query to display all events
if ($event_result = $con->query("SELECT Event.Name FROM event")) {
echo "<form method =\"POST\">";
while ($row = $event_result->fetch_assoc()) {
echo $row['Name'] . ' <br> ';
if ($student_result = $con->query("SELECT Student.Form, Teacher.Form, Student.Forename, Student.Surname, Student_ID " .
"FROM Student, Teacher " .
"WHERE Student.Form = Teacher.Form AND Teacher.Form = 'C'")) {
if ($student_result->num_rows) {
echo "<select name ='Student_ID'>";
while ($row1 = $student_result->fetch_assoc()) {
echo "<option value ='" . $row1['Student_ID'] . "'>" . $row1['Forename'] . ' ' . $row1['Surname'] . "</option>";
}
echo "</select>";
}
}
}
echo "</form>";
}
?>
You have an opening <br /> as you echo out the name of the row. This means echo the title, then break before you display the <select> group. You should move that <br /> down right before the closing while loop.
Or just remove the top <Br /> and add it back after the ending so it looks like this:
echo "</select> <br />";
edit: this will fix the last two of your questions, as now each select group will properly line up with the title of the row. If you want more space at the top of the form you have a bunch of options: add more breaks, add a margin or padding, or position the element as relative and use css top property or CSS transforms to move it.

Some html tags' parameters messup html/php code

When I create/edit post to my website, and when I use html tags (either type it by my self or using WYSIWYG editor (in this case nicEdit, but I've used different one - same problem)) it does not show anything on the page.
to be more specific: I have article container which imports php code, which than iterates through the db table and echo posts. This is the code:
<h3>Wydarzenia</h3>
<?php
//collects data from table "events" and sort it by "id" date in descending oreder
$event_table_data = mysqli_query($db_con, "SELECT * FROM events ORDER BY id DESC")
or die(mysqli_error());
//puts the data from "events" into an array
$event_content_array = mysqli_fetch_array($event_table_data);
//makes variables from table columns
$id = id;
$title = title;
$happeninng_date = happen;
$created_date = created;
$content = content;
$author = author;
//add new post button
if((isset($_SESSION['sess_wizardrights'])) && ($_SESSION['sess_wizardrights'] == true)){
echo "<a class='e_event_addpost_link' href='./index.php?link=add_post'><button class='e_event_addpost_button' type='button'>Dodaj nowy post</button></a>";
}
do{
//echo each event for main event article
echo "<span class='b_events_span' id='" . $event_content_array[$id] . "'>";
echo " <a class='b_events_anchor' id='" . $event_content_array[$id] . "'></a>";
echo " <h2 class='b_events_title'>" . $event_content_array[$title] . "</h2>";
if((isset($_SESSION['sess_wizardrights'])) && ($_SESSION['sess_wizardrights'] == true)){
echo "<form class='b_events_edit_form' name='edit_post' action='./index.php?link=edit_post' method='post'>";
echo " <input type='hidden' name='id' value='" . $event_content_array[$id] . "'> ";
echo " <input class='e_events_edit_submit' type='submit' value='Edytuj wydarzenie'>";
echo "</form>";
}
echo " <div class='fb-share-button' data-href='http://www.zhppgkaberdeen.co.uk/index.php?link=events#" . $event_content_array[$id] . "'></div>";
echo " <p class='b_events_happening_date'><i>Data wydarzenia: " . $event_content_array[$happeninng_date] . "</i></p>";
echo " <p class='b_events_content'>" . $event_content_array[$content] . "</p>";
echo " <p class='b_events_created_date'>Utworzono: " . $event_content_array[$created_date] . "</p>";
echo " <p class='b_events_author'><b>Czuwaj!</b><br/>" . $event_content_array[$author] . "</p>";
echo "</span>";
}while($event_content_array = mysqli_fetch_array( $event_table_data ));
?>
When I add tags like <img> for instance with alt="" parameter it completely ruins web page. What I mean by that is that the article container is not shown at all (I've checked that with firebug) as well as everything after the article tag (like footer for instance). What is even more strange is that this: alt="text" did not break the page but every other random word does, how ever it does not show the "text" at image at all.
Similar thing happen when I tried to create table within the post and title or target parameter, I cant remember which one.
I have tried to use htmlentities($event_content_array[$content]), htmlspecialchar($event_content_array[$content]) and mysqli_real_escape_string($do_con, $event_content_array[$content]) -> non of this helped.
Is there a way to fix that or I just need to give up using this tags/parameters?
Thank you for any help or explanation
if you are using e.g echo "<img alt="text">";try to escape the " such as alt=\"text\"

insert masonry in php from images that were retreieved from a mysql database

I would like to insert masonry into a php form that retrieves images from a MySQL database. I can retrieve the images, but when I try to put in masonry, the whole thing doesn't work at all.
Here is my code
$result = mysqli_query($con,"SELECT * FROM img");
while($row = mysqli_fetch_array($result)) {
echo "<script src="/path/to/masonry.pkgd.min.js"></script>"
echo "<div id="container">"
echo "<img class='item' src='" . $row['path'] . "' />";
echo "</div>"
}
mysqli_close($con);
?>
You have 3 issues with that code:
First of all, you don't really have to include your script for every new <div>. Just include your script once in the <head> section, as follows:
<html>
<head>
<script src="/path/to/masonry" />
...
</head>
Secondly, you need to differentiate between " as a string container in your PHP code and as a character that should appear in your final HTML code. When using it as the latter, you need to escape it by adding a backslash (like so: \"), or simply replace it with an apostrophe:
$result = mysqli_query($con,"SELECT * FROM img");
while($row = mysqli_fetch_array($result)) {
echo "<div id='container'>";
echo "<img class='item' src='" . $row['path'] . "' />";
echo "</div>";
}
mysqli_close($con);
?>
And finally, as presented in the example, you forgot the semicolon terminator at the end of your lines of code inside the while loop.

How to create a series of DIVs using PHP + mySQL?

I'm looking to create a formatted product list from an SQL database. My aim is to have a store on my website with a series of small boxes containing some shorthand information about each product, that when clicked will open a pop-up containing detailed information. (I have a working Javascript/JQuery code to create the pop-ups.)
Here is the PHP code so far, simply to get the information from the database and display it on a webpage...
(I've been using XAMPP to provide an environment for me to test the code in)
<?php
mysql_connect("localhost", "root", "") or die (mysql_error ());
mysql_select_db("Database1") or die(mysql_error());
$strSQL = "SELECT * FROM Products";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {
echo $row['Brand'] . " " . $row['ProductName'] . " " . $row['Image'] . "<br />";
}
mysql_close();
?>
I want the echoed line to be displayed in a divider, with a divider generated for each record in the SQL database (say I have 10 products available, there would be ten dividers, and 10 different boxes on the webpage). The divider's class is "ProductBox".
echo "<div class=\"ProductBox\">"; $row['Brand'] . " " . $row['ProductName'] . " " . $row['Image'] . "</div>";
This was the closest I have come to a solution, which was simply managing to write a code with no syntax errors - alas, nothing actually displays on the webpage.
If I'm going about this entirely the wrong way please tell me - I'm fairly sure I need to use a SQL database to dynamically update stock on a live website, but if I need to implement a different programming language or whatever then just tell me what you think would work and help me with a solution.
You have an extra semicolon in your code
echo "<div class=\"ProductBox\">"; $row['Brand'] . " " . $row['ProductName'] . " " . $row['Image'] . "</div>";
Replace with
echo "<div class=\"ProductBox\">". $row['Brand'] . " " . $row['ProductName'] . " " . $row['Image'] . "</div>";
mysql_fetch_array needs to be used like this (see PHP Doc):
while($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
}
or you could just use "mysql_fetch_assoc" instead.
HOWEVER, if you're new to PHP, I HIGHLY RECOMMEND that you get started on the right foot. mysql_query functions are soon to be deprecated. DON'T USE THEM. Most recommend using "PDO" for querying your database. Here's a great tutorial to teach you: http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers
Also, as mentioned, you have an extra semi-colon.
Dont forget these basics markups :
`<HTML>
<HEAD>
</HEAD>
<BODY> put in here your divs
</BODY>
</HTML>`

PHP and MySQL content displaying out of order

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

Categories