<?php
require 'db.php';
include_once("header.php");
include_once("functions.php");
include_once("profile.php");
if(isset($_POST['search_term'])){
$search_term = mysql_real_escape_string(htmlentities ($_POST['search_term']));
if(!empty($search_term)){
$search = mysql_query("SELECT `username`,`id` FROM `users` WHERE `username` LIKE '%$search_term%' and `business` <> 'business'");
$result_count = mysql_num_rows($search);
$suffix = ($result_count != 1) ? 's' : '';
echo '<div data-theme="a">Your search for <strong>' , $search_term ,'</strong> returned <strong>', $result_count,' </strong> record', $suffix, '</div>';
while($results_row = mysql_fetch_assoc($search)){
echo '<div data-theme="a"><strong>', "<img src='/image/<?php echo $image; ?>' width= 50px height=50px>", $results_row['username'], '</strong></div>';
$following = following($_SESSION['userid']);
if (in_array($key,$following)){
echo ' <div action= "action.php" method="GET" data-theme="a">
<input type="hidden" name="id" value="$key"/>
<input type="submit" name="do" value="follow" data-theme="a"/>
</div>';
}else{
echo " <div action='action.php' method='GET' data-theme='a'>
<input type='hidden' name='id' value='$key'/>
<input type='submit' name='do' value='follow' data-theme='a'/>
</div>";
}
}
}
}
?>
I would like some help putting the user image into the echo section of this code. I am not exactly sure how to do this so that it puts the image on the correct line of the search. Any advice would be greatly appreciated. Below is the line of code that I am referring too. Thanks.
while($results_row = mysql_fetch_assoc($search)) {
echo '',
"' width= 50px
height=50px>", $results_row['username'], '';
I don't see $image defined anywhere in your code, so I'm going to assume the image is being pulled from the database.
If that's the case then you'll want to do something like this:
while($results_row = mysql_fetch_assoc($search)){
echo '<div data-theme="a"><strong><img src="/image/'.$results_row['image'].'" style='width:50px;height:50px' />'.$results_row['username'].'</strong></div>';
}
just write:
..."<img src='/image/<?php echo $image; ?>' width='50' height='50'>", ...
but are you sure that 50x50 is suitable? you might want to set only width=50 and leave the browser to set height accordingly.
$image = 'someImage.png';
echo '<div data-theme="a"><strong>', "<img src='/image/{$image}' width= 50px height=50px>", $results_row['username'], '</strong></div>';
You would want something like this:
<?php
while($results_row = mysql_fetch_assoc($search)){
?>
<div data-theme="a">
<img src="/image/<?php echo $image; ?>" width="50px" height="50px">
<strong>
<?php echo $results_row['username']; ?>
</strong>
</div>
<?php
}
?>
A few things to check though:
dont use tags to wrap tags. They're generally used for text only
You're not selecting any image on your query.
don't use echo to output html its ugly.
don't use GET.
Use prepared statements (PDO or mysqli) to protect from mysql injection.
Related
I have a problem in a file : movies.php
I want to show all movies on the files when there is no id, and if the id exists, i want to show the movie with that id , i used :
echo "<div id='head'>$title</div>";
echo "<div id='bodyar'>$content</div> <br />
<hr>Category : <span class='date'>$moviecategory</span></hr>
<hr>Views : <span class='date'>$views_numimg</span></hr>
<hr></hr> <br />"; exit;}
$orderposts = mysql_query("select * from movie ");
echo "<div class='bodypanelposts'>";
while ($rowar = mysql_fetch_assoc($orderposts)) {
$id_po = $rowar['id'];
$picture = $rowar['picture'];
$title = $rowar['title'];
echo "<div id='movieall'><table id='classing' border='0'
cellspacing='2'><tr> <td>";
echo "<a href='movies.php?id=$id_po'><img src='$picture' alt='$image_caption' width='180' height='250'><br /></div><div class='movies'>$title</div></a><br />LIKE BOX GOES HERE</tr></td></table></div>";
}
The problem is , after using that , the footer is not appearing anymore ..
I want it to appear.
To let PHP know it has to start interpret the code, you need start tags:
<?php
// PHP code here
?>
You should also concat variables by a dot instead of putting into the quotes:
echo "<div id='head'>" . $title . "</div>";
(Some might say this is not important but it is IMO, PHP can't handle it properly in every case.)
When using exit;, you tell PHP to quit and flush the result to the browser.
There is also a closing } bracket after the exit, but I don't see any opening { bracket.
A better way to handle your HTML is to do it like this:
<div id='head'><?=$title?></div>
<div id='bodyar'><?=$content?></div>
<br />
<table>
<tr><td>Category</td><td><span class='date'><?=$moviecategory?></span></td></tr>
<tr><td>Views</td><td><span class='date'><?=$views_numimg?></span></td></tr>
</table>
<div class='bodypanelposts'>
<?php
while ($rowar = mysql_fetch_assoc($orderposts)) {
$id_po = $rowar['id'];
$picture = $rowar['picture'];
$title = $rowar['title'];
echo <<<HTML
<div id='movieall'>
<table id='classing' border='0' cellspacing='2'>
<tr><td><a href='movies.php?id=$id_po'><img src='$picture' alt='$image_caption' width='180' height='250'><div class='movies'>$title</div></a>
<br />LIKE BOX GOES HERE
</td></tr>
</table>
</div>
HTML;
?>
</div>
Notice the <?= tags to do inline PHP echo statements, allowing you to write HTML without having to wrap them in echo statements.
You can also use HEREDOC syntax to echo out a large chunk of HTML with variables inline.
These two methods make it much easier to reason about what your code is outputting.
EDIT: THanks everyone for pointing out that i override the $Html variable. i've had a bit to drink tonight and missed that.
I'm working on my own custom forum for my website and for some reason or another the code is only returning one row. There's two "posts" on the forum, and only the lastest of them is being returned in when echoing the results of this function.
function displayTopics($tid)
{
$sql = "SELECT message_body, poster FROM thread_messages WHERE threadID = :tid";
$que = $this->db->prepare($sql);
$que->bindParam(':tid', $tid);
try{
$que->execute();
while($row = $que->fetch(PDO::FETCH_OBJ))
{
$username = $this->getUsername($row->poster);
$html = "<div class='message'>
<div class='userInfo'>
<img width='50' height='50' />
{$username}
</div>
<div class='body'>
{$row->message_body}
</div>
</div>";
}
}catch(PDOException $e){}
$html .=
"<form action='reply.php' method='post'><input type='hidden' name='tid' value='{$tid}'>".
"<textarea name='replybody'>Reply...</textarea>".
"<input type='submit' value='reply'>".
"</form>";
return $html;
}
I messed up, my bad. the loop should've looked like this
while($row = $que->fetch(PDO::FETCH_OBJ))
{
$username = $this->getUsername($row->poster);
$html .= "<div class='message'>
<div class='userInfo'>
<img width='50' height='50' />
{$username}
</div>
<div class='body'>
{$row->message_body}
</div>
</div>";
}
I was overriding the $Html variable.
This question already has answers here:
Build SELECT query with dynamic number of LIKE conditions as a mysqli prepared statement
(2 answers)
Closed 1 year ago.
I have created search engine so when someone type in search box it takes data from my database and show the results in images.
Everything is working fine but if I have data in database with Name like 'Lamborghini', when someone type in search box 'lambo' or 'Lamborghini' it is working fine and show the result but when he types 'Lamborghini car' it is not showing any result.
Here is my PHP Code:
<html>
<link href="css/imgpages.css" rel="stylesheet" type="text/css">
<head>
<title>Search the Database</title>
</head>
<body>
<form action="" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
<?php
include("connection.php");
$term = $_POST['term'];
$query = mysql_query("select * from save_data where Title like '%$term%'");
while($row = mysql_fetch_array($query)) {
$post_id = $row['ID'];
$title = $row['Title'];
$image = $row['Name'];
?>
<div id="body">
<a href="pictures.php?title=<?php echo $title; ?>">
<div id="title"><?php echo $title; ?></div></a>
<a href="pictures.php?title=<?php echo $title; ?>">
<img src='uploads/<?php echo $image; ?>' width='140' height='140'></a>
</div>
<?php } ?>
You should:
fix your SQL (make it secure - just one search could delete your whole database if you leave it like this, switch to mysqli or PDO because mysql_* functions are deprecated)
split your query string into separate words and decide how to handle them (whether you'll use OR or AND... in other words, do all search terms need to match, or any of them, etc.)
When you deal with all that and decide to make it more advanced, you should learn about full-text searching.
It is 'cause { %$term% } means the query try search something that contain all of the string in $term. Try this :
$term = explode(" ",$term);
if (count($term) > 0) {
$Where = '';
foreach($term as $Item) {
$Where .= "Title like '%$Item%' OR ";
}
$Where = substr($Where,0,-4);
$query = mysql_query("SELECT * FROM `save_data` WHERE $Where");
}
In this way the search will check out all words.
As the above people said just Explode the input string into an array and search for documents(description, keywords) where most of the elements match. Also take care of SQL Injection.
first make your term search explode so it would go into an array
<?php
$term = $_POST['term'];
$words = explode(" ", $term);
?>
then create a forearch loop for the mysql_query of your search and implode with "OR"
<?php
foreach ($words as $words)
{
$queries[]="select * from save_data where Title LIKE '%" .
mysql_real_escape_string($words) . "%'";
}
$query=implode(' OR ' ,$queries);
$results=mysql_query($query);
while($row = mysql_fetch_array($results))
{
$post_id = $row['ID'];
$title = $row['Title'];
$image = $row['Name'];
?>
<div id="body">
<a href="pictures.php?title=<?php echo $title; ?>">
<div id="title"><?php echo $title; ?></div>
</a>
<a href="pictures.php?title=<?php echo $title; ?>">
<img src='uploads/<?php echo $image; ?>' width='140' height='140'>
</a>
</div>
<?php } ?>
Hey iam making a slot machine and is almost done. The only thing i need is the credit's to stay so it just add points on the credit's. Like if i have 100 credits and then get 25 credits i want it to say 125 credits. Now i don't know how to get the credits from the round before.
This is what i got:
<?
$tal = rand (1,3 ); {
echo "<img src='css/billeder/enarmet$tal.gif' class=billed />";
$tal2 = rand (1,3 );
echo "<img src='css/billeder/enarmet$tal2.gif' class=billed />";
$tal3 = rand (1,3 );
echo "<img src='css/billeder/enarmet$tal3.gif' class=billed />"; }
?>
</div>
<div id="credits">
<h3 id="credits2">CREDITS</h3>
<h3 id="credits3"><?php
$credits=$_GET['credits'];
if ($tal . $tal2 . $tal3 == 111){
($credits=($credits+100));
}
if ($tal . $tal2 . $tal3 == 222){
($credits=($credits+50));
}
if ($tal . $tal2 . $tal3 == 333){
($credits=($credits+25));
}
echo $credits;
?></h3>
</div>
</div>
<form action="index.php" method="POST">
<input type="submit" value="SPIN" class="knap">
</form>
<form action="cashout.php" method="POST">
<input type="submit" value="CASH OUT" class="knap">
</form>
</div>
What about using sessions? Store and retrieve credits from session storage using
session_start();
$credits = $_SESSION['credits'];
at the top of your script and
$_SESSION['credits'] = $credits;
at the bottom.
This way credits will be preserved between page-loads.
You can remove the curly braces at the end of the following lines, its not need and try it.
<?
$tal = rand (1,3 ); { // remove the open brace
echo "<img src='css/billeder/enarmet$tal.gif' class=billed />";
...
echo "<img src='css/billeder/enarmet$tal3.gif' class=billed />"; } // remove the close brace
?>
I have the following code that sets a background if user has uploaded to database. If user has NOT uploaded an image then the result is a blank img src=''
I need to set this as an if isset function so I can plug in an alternate image if user has not uploaded anything.
Here is the current code:
<div id="background"><?php echo isset($background_image) && file_exists(ROOT.$background_image)?"<img src='$background_image' alt='' />":'';?></div>
Your code's a little dirty, opening php and closing it mid-html tag is only going to make it confusing for you in the future.
You're echoing back an isset which is just echo'ing back a boolean.
Try this;
$background_image = ""; // Not sure what you're using here - their username? Dump it in here anyway.
if (file_exists($background_image))
{
echo " <div id=\"background\">
<img src=\"{$background_image}\" alt=\"\" title=\"\" />
</div>";
}
Hope this helps.
Eoghan
I'm not sure what you mean, but a another aproach to what you are trying to do would be:
<div id="background">
<?php
$optionalImage = 'background.png';
$userImage = getUserImage();
if(empty($userImage)) {
$userImage = $optionalImage;
}
?>
<img src="<?php echo $userImage; ?>" />
</div>
Is it really neccessary to set the blank source of the image?
But a understandable and corrected code of what you are attempting is this
<div id="background">
<?php
echo "<img src='";
echo isset($background_image) && file_exists(ROOT.$background_image) ? $background_image : '';
echo "' alt='' />";
?>
</div>
The problem was that, either you were echo entire <img> tag, or just display ' '(Blank) with attached endings.
The short form:
echo "<img src='".(isset($background_image) && file_exists(ROOT.$background_image) ? $background_image : '')."' alt='' />";