I'm trying to make a facebook wall-alike php script.
It loads the posts stuff, photo url and user stuff like username and user id from my MySQL database. It also checks if the post is just a comment to another post (parentof). I can present all the original posts with pictures with a simple do-while using tags .
But how could I present the comments under every post? I guess with another do-loop, but how?
Here is my code:
<?php
require_once "config.php";
$result = mysql_query('SET NAMES utf8');
$result = mysql_query('SET CHARACTER SET utf8');
$wallhaku = mysql_query("SELECT `wall`.`post_id`, `wall`.`parentof`, `wall`.`sentby`, `wall`.`text`, `wall`.`image_url`, `users`.`username`, `users`.`photopath`, `users`.`name`, `users`.`member_id` FROM `wall` LEFT JOIN `users` ON `wall`.`sentby` = `users`.`member_id` WHERE parentof=0 ORDER BY post_id DESC") or die (mysql_error());
$row_wallhaku = mysql_fetch_array($wallhaku);
<table width="800" height="120" border="0" cellpadding="10">
<?php $i=0; $numberpage=1;
do {
$wallid = $row_wallhaku['post_id'];
$parenthaku = mysql_query("SELECT post_id, parentof FROM wall WHERE parentof=$wallid") or die (mysql_error());
$row_parenthaku = mysql_num_rows($parenthaku);
<td width="120" align="left">
<img src=<?php echo $row_wallhaku['photopath']; ?> height= "100" width="100">
</td>
<td width="600" align="left">
<?php echo $row_wallhaku['name']?><br /><p><?php echo $row_wallhaku['text'];
if($row_wallhaku['image_url']=="0") {
<p align="right"><?php echo $row_parenthaku ?> kommenttia. Lue, kommentoi <?php } ?></td>
<?php $i++; if($i%$numberpage==0) echo "</tr>";
if($row_wallhaku['image_url']!="0")
{
<tr>
<td width='800' colspan='2' align='center'>
<img src=<?php echo $row_wallhaku['image_url']; ?> height="180">
<p align="right"><?php echo $row_parenthaku ?> kommenttia. Lue, kommentoi
</td>
</tr>
<?php
}
</table>
In case that the relation between posts is one to many , You need to use 2 queries.
The first query will get the data of the topic (the post).
The other query will get the data of all the comments which "parentof" equals the topic's id.
$id = $_GET['id'];
$getTopic = mysql_query("SELECT * FROM posts WHERE id='$id'");
$topic = mysql_fetch_array($getTopic);
$getComments = mysql_query("SELECT * FROM posts WHERE parentOf='{$topic['id']}'");
while($comment = mysql_fetch_array($getComments))
{
echo $comment['title']; //You should use a "view" functin which handles the HTML and staff.
}
Related
Thank you all in advance....
I am doing a project in which I have a field to store the image. In that form, the image uploaded is completed with croppie plugin. and I stored the data in base 64 encodings. But I cannot fetch it to a page called view.php but the images are fetched to the form after cropping.
Please help me to figure it out the mistake that I have done
<tbody>
<?php
$no = 1;
$data = mysqli_query($con, "SELECT * FROM `register` WHERE app_registration IS NULL ORDER BY `app_id` DESC ");
while ($row = mysqli_fetch_assoc($data)) {
?>
<tr>
<td><?php echo $row['app_id'] ?></td>
<?php if (!empty($row['image_reference_id'])) {
$data1 = mysqli_query($con, "SELECT * FROM `photo_table` WHERE image_unique_id = '" . $row['image_reference_id'] . "'");
$row1 = mysqli_fetch_assoc($data1)
?>
<td><img src="data:image/jpg;base64,'.base64_encode($row['images']).'"/></td>
<?php } else {
?>
<td><img src="../images/<?php echo $row['app_image'] ?>" style="width: 100px; height: 100px;"></td>
<?php } ?>
<td><?php echo $row['app_name'] ?></td>
<td><?php echo $row['app_mobile_no_1'] ?></td>
</tr>
<?php } ?>
</tbody>
Note: In db the images are in Long Blob
You will need to change the following:
<img src="data:image/jpg;base64,'.base64_encode($row['images']).'" />
to
<img src="data:image/jpg;base64, <?php base64_encode($row['images']);?>" />
Here's a screenshot of the page that I want to put a pagination Below is my code and I want to create a simple pagination. I tried some examples available in this site but unfortunately it doesn't work for me or I might have missed something in the code.
<?php
session_start();
$server = 'localhost';
$user = 'root';
$pass = 'root';
$connect = mysql_connect($server,$user,$pass)
or die(mysql_error());
$selectdb = mysql_select_db('des')
or die(mysql_error());
?>
<form method="post" name="action_form" action="admin2.php">
<div id="gallery1" class="lbGallery">
<table class="table" width="100%" cellpadding="10">
<tbody>
<?php
$allRecords = mysql_query('select * from cms ORDER BY id DESC limit 4');
if(is_resource($allRecords))
{
while($row = mysql_fetch_assoc($allRecords))
{
?>
<tr><ul>
<td width="30%"><li style="list-style:none"><a href="uploads/<?php echo $row['image'];?>"/><img src="uploads/<?php echo $row['image'];?>"/></li></td>
<td style="float:left; font-size:16px"><?php echo $row['name']; ?></td>
</ul>
</tr>
<?php
}
}
?>
</tbody>
</table>
</div>
</form>
Try this. What is does is:
create a contant $amount en a variable $offset
Create a link (next) with which sends the value of $offset back to the script
Catch the value in $_GET['offset'];
Add the value of $amount to $offset to create a new offset.
Run MySQL statement again with the new values for LIMIT
I didn't actualy test this code for typo's, but you'll get the general idea. Hope this is of any help.
(Best to use the new mysqli statement by te way).
<?php
$amount = 4;
if (!empty($_GET['offset']))
{
$offset = $_GET['offset'] + $amount;
}
else
{
$offset = 1;
}
$allRecords = mysql_query('select * from cms ORDER BY id DESC limit $amount,$offset');
if(is_resource($allRecords))
{
while($row = mysql_fetch_assoc($allRecords))
{
?>
<tr><ul>
<td width="30%"><li style="list-style:none"><a href="uploads/<?php echo $row['image'];?>"/><img src="uploads/<?php echo $row['image'];?>"/></li></td>
<td style="float:left; font-size:16px"><?php echo $row['name']; ?></td>
</ul>
</tr>
<tr>
<td colspan="2">
Next
</td>
</tr>
<?php
}
}
?>
how to display blank if there is no image in a record.As i have inserted a record in database without an image but while fetching an record it is displaying an blank image in front end.It should not show any image if there is no image.Here is my code. If there is no image it should show only description.
Blogimage.php
<tbody>
<?php include "blogs.php" ;
while($row = mysql_fetch_array($result))
{?>
<tr>
<td><img src="admin/upload/<?php echo $row['image'];?>" height="100" style="width:60%;height:50%;"/></td>
</tr>
<tr>
<td><?php echo "<p style='width:60%;'>" .$row['blog_description']."</p>"; ?></td>
</tr>
<?php }?>
</tbody>
Blogs.php
$id=$_GET['title'];
$res = "SELECT * FROM blogs
WHERE blog_title='$id'";
$result=mysql_query($res);
try changing this
<img src="admin/upload/<?php echo $row['image'];?>" height="100" style="width:60%;height:50%;"/>
to this
<?php if($row['image']) echo "<img src='admin/upload/".$row['image']."' height='100' style='width:60%;height:50%;' />"; ?>
A couple of general points before we go into this
1) The MySql library you are using is old and busted - you should be using something newer - read this - the options are Mysql PDO and MySqli - I promise you, you will be glad you did, i use IDIORM as an interface and find it very good (Also helps prevent stuff in point 2 below!)
2) Your code can be SQL injected - more about that here - this is very bad :)
Below is an example, I have fixed the SQL injection problem, and put in some logic that would test to see if there is an image, if not, it will dump out a 'noimage.jpg' src value.
I have removed the include, and just simply put the code for blogs.php inline.
<tbody>
<?php
//START CODE FOR BLOGS.PHP
$query = sprintf("SELECT * FROM blogs WHERE blog_title='%s'",
mysql_real_escape_string($_GET['title']));
$result = mysql_query($query);
//END CODE FOR BLOGS.PHP
while($row = mysql_fetch_array($result))
{
if(!empty($row['image']) && strlen($row['image']) > 4)
{
$iamgeSrc = 'admin/upload/' . $row['image'];
}
else
{
$imageSrc = 'admin/upload/noimage.jpg';
}
?>
<tr>
<td>
<img src="<?php echo $imageSrc; ?>" height="100" style="width:60%;height:50%;"/>
</td>
</tr>
<tr>
<td>
<?php echo "<p style='width:60%;'>" .$row['blog_description']."</p>"; ?>
</td>
</tr>
<?php
}
?>
</tbody>
If you want no image to show at all, then this would work
<tbody>
<?php
//START CODE FOR BLOGS.PHP
$query = sprintf("SELECT * FROM blogs WHERE blog_title='%s'",
mysql_real_escape_string($_GET['title']));
$result = mysql_query($query);
//END CODE FOR BLOGS.PHP
while($row = mysql_fetch_array($result))
{
$image = '';
if(!empty($row['image']) && strlen($row['image']) > 4)
{
$image = '<img src="admin/upload/'.$row['image'].'" height="100" style="width:60%;height:50%;"/>';
}
?>
<tr>
<td>
<?php echo $image; ?>
</td>
</tr>
<tr>
<td>
<?php echo "<p style='width:60%;'>" .$row['blog_description']."</p>"; ?>
</td>
</tr>
<?php
}
?>
</tbody>
I want to make a horizontal bar chart in a web-page using php,mysql,javascript,css,html and 'wamp server',editor: 'Macromedia Dreamweaver',browser: 'Mozilla Firefox';
i want to read data(semister results) from table,and display data through bar chart like,
Database name exam
Table name 'sem_result' contain following columns>> regno[Primary key], uid, reg_date, exam_date, sem, result;
php code is::
<?php
// Connect to server
$cn=mysql_connect("localhost", "root", "");
//Connect to Database
mysql_select_db("exam") or die(mysql_error());
//sql query
$sql="SELECT result FROM sem_result WHERE uid=11111";
//collect results
$result=mysql_query($sql,$cn);
//read data if found
$counter=0;
while($rd=mysql_fetch_array($result))
{
$sem_result[$counter]=$rd[0]; //save sem results into array
$counter=$counter+1;
}
//display
echo "<table>
<tr>
<td>sem1</td>
<td width='100px'>
<img src='img/menu_back.png' width='".$sem_result[0]."%' height='15px'/>
</td>
<td>".$sem_result[0]."%</td>
</tr>
<tr>
<td>sem2</td>
<td width='100px'>
<img src='img/menu_back.png' width='".$sem_result[1]."%' height='15px'/>
</td>
<td>".$sem_result[1]."</td>
</tr>
</table>";
//close database
mysql_close($cn);
?>
if results are 78.95%,78.10% ,bar chart shows both result are equal, i.e 78%; image width become 78%,not 78.95% please help to fix this problem.
change table name and column name and progress.png image.
after that use it.
`
<?php $s="select count(*) as tnum from leakage";
$r=mysql_query($s) or die (mysql_error());
while($rw=mysql_fetch_array($r))
{
echo $tno=$rw['tnum'];
}
?>
<table class="table table-hover">
<thead>
<tr>
<th>DEPARTMENT</th>
<th>No.Of Leakage</th>
</tr>
</thead>
<?php
$sql1="select dept,count(dept) as total from leakage where dept='winding'";
$result1=mysql_query($sql1) or die(mysql_error());
while($row=mysql_fetch_array($result1))
{
$dept=$row['dept'];
$tot=$row['total'];
}
?>
<tr>
<td><?php echo $dept; ?></td>
<td width="100%"><img src="assets/images/progress.png" width="<?php echo (($tot/$tno)*100);?>" height="20px"><?php echo $tot;?>%</td>
</tr>
<?php
$sql2="select dept,count(dept) as total from leakage where dept='spining'";
$result2=mysql_query($sql2) or die(mysql_error());
while($row=mysql_fetch_array($result2))
{
$dept=$row['dept'];
$tot=$row['total'];
}
?>
<tr>
<td><?php echo $dept; ?></td>
<td width="100%"><img src="assets/images/progress.png" width="<?php echo (($tot/$tno)*100);?>" height="20px"><?php echo $tot;?>%</td>
</tr>
<?php
$sql5="select count(dept) as total from leakage";
$result5=mysql_query($sql5) or die(mysql_error());
while($row=mysql_fetch_array($result5))
{
$tot=$row['total'];
}
?>
<tr>
<td>Total</td>
<td width="100%"><img src="assets/images/progress.png" width="<?php echo $tot;?>" height="20px"><?php echo $tot; ?></td>
</tr>
</table>
`
As #hakre already pointed out, you can't get any more precise than a single pixel or percentage, ie you can't have 78.5px or 78.5%... however, you still have some options without javascript. First, I'm not sure why you are using an image for the green bar, why not just pure html/css? Either way, here's my suggestion:
Get the total width of the bar graph, from 0 - 100, in pixels. Just by eyeballing the image you posted, I'm gonna call it 325px:
$total_width = '325';
Then, set each bar's width as such:
$bar_width = round($total_width * ($sem_result[0] / 100));
This way, a stored value of 78.10% will end up as 254px, and 78.95% will be 257px. It is still not the exact percentage, but it is closer. The longer the total width of your graph, the more precise you calculation will be.
I am working on a simple inbox/pm system, and I can't figure out why but I can get the display working for sent messages, I can display the list of sent items, view pms from the inbox, but not sure what I am doing wrong, any tips appreciated..
here is my code:
<table>
<?php
$id = $_GET['id'];
$from_user = $_SESSION['user_id'];
$sql = "SELECT users.user_id, users.username, users.profile, messages.id, messages.to_user, messages.from_user,
messages.subject, messages.message, messages.has_read, messages.deleted, messages.date_sent
FROM `messages`
JOIN `users` ON messages.to_user = users.user_id
WHERE messages.from_user = '$from_user' AND messages.id = '$id' ORDER BY messages.date_sent DESC";
$result = mysql_query($sql);
$rows = mysql_fetch_array($result);
$from_user = $rows['from_user'];
$subject = $rows['subject'];
?><tr>
<td width="50px" align="center">
<img src="<?php echo $rows['profile']; ?>" width="40px"><br><?php echo $rows['username']; ?>
</td>
<td valign="top" width="350px">
<b><?php echo $rows['subject']; ?></b><br>
<?php echo $rows['message']; ?>
</td><td><?php echo $rows['date_sent']; ?></td>
</tr>
<tr>
<td colspan="3"><hr></td>
</tr>
</table>
Your have $from_user specified twice, try removing the second and see how you go.