while($info = mysql_fetch_array( $result ))
{
//Outputs the data
Echo "<b>".$info['name'] . " </b> :-<br>";
include('db.php');
$postid = 1;
$data = mysql_fetch_object(mysql_query('SELECT `like` , `unlike` FROM posts WHERE id=" '.$postid.' " '));
Echo "<html><body><a href='javascript:;' onclick='doAction($postid,like);'>Like (<span id='<?php echo $postid;?>_likes'>$data->like</span>)</a></html></body>";
Echo "<html><body><a href='javascript:;' onclick='doAction($postid,unlike);'>Unlike (<span id='<?php echo $postid;?>_unlikes'>$data->unlike</span>)</a></html></body>";
}
In the above script I have included like and unlike option with every fetched result.
I get the following result: like (0)unlike (0)
Every thing works perfectly. The only problem is that it doesn't increment when clicked on.
Please help me.
It works perfect in the simple code shown below:
<html>
<head>
<script src="http://code.jquery.com/jquery-1.3.2.min.js"></script>
<script>
function doAction(postid,type){
$.post('doAjax.php' , {postid:postid, type:type}, function(data){
$('#'+postid+'_'+type+'s').text(data);
});
}
</script>
</head>
<body>
<?php
include('db.php');
$postid = 1;
$data = mysql_fetch_object( mysql_query( 'SELECT `like` , `unlike` FROM posts WHERE id= " ' . $postid . ' " ' ) );
?>
<p>hello</p>
Like (<span id="<?php echo $postid; ?>_likes"><?php echo $data->like; ?></span>)
Unlike (<span id="<?php echo $postid; ?>_unlikes"><?php echo $data->unlike; ?></span>)
</body>
</html>
I also want to use PDO instead of mysql_fetch for the script shown above .
Thanks in advance.
Try replacing the 2 lines "Echo <html...> with:
echo "<div><a href='javascript:;' onclick='doAction($postid,like);'>Like (<span id='{$postid}_likes'>{$data->like}</span>)</a></div>";
echo "<div><a href='javascript:;' onclick='doAction($postid,unlike);'>Unlike (<span id='{$postid}_unlikes'>{$data->unlike}</span>)</a></div>";
Other issues:
-avoid using mysql_query, use PDO as suggested
-the query inside a loop will lead to poor performance. you should have a single query that gets the info and the likes (using JOINs). And inside the loop display the likes for each info.
Related
I have selecting data from MySQL table and returned data looks like:
Name Rank Vessel Type Preview
John 1 ab View
Lisa 1 cd View
Carl 2 bd View
As you see above View should be link to user's profiles depending on their Id.
For now I'm using table as you see below and this is line for View link:
<?php echo "<td> View</td>"; ?>
I don't know why, but It redirecting users like: www.etc.com/preview.php?Id= without provided Id.
If I'm using something like:
<?php echo "<td> View " . $Id . "</td>"; ?>
It returning View Id for example View 1, that's mean $Id variable is fine. Have you ideas what's wrong?
Also I want to ask you If it is correct way for redirecting? Or there is any other / better way to do that?
This is PHP:
<?php
if(isset($UserID)) {
$users = $con->prepare("
SELECT DISTINCT Id,
CONCAT(FirstName, ' ', LastName) AS FullName,
RankApplied,
VesselsType
FROM tbl
");
$users->execute();
$users->bind_result($Id, $FName, $RankApplied, $VesselsType);
} else {
echo "There is no User ID detected, try to refresh browser.";
}
?>
<table>
<tr>
<th>Name</th>
<th>Rank</th>
<th>Vessel Type</th>
<th>Preview</th>
</tr>
<?php
while ($users->fetch()) {
?>
<tr>
<td><?php echo $FName; ?></td>
<td><?php echo $RankApplied; ?></td>
<td><?php echo $VesselsType; ?></td>
<?php echo "<td> View</td>"; ?>
</tr>
<?php
}
?>
</table>
You are closing your href quote before including the id, change the line to the following to get it to work:
<?php echo "<td> <a href='preview.php?Id=" . $Id . "'>View</a></td>"; ?>
Replace
<?php echo "<td> View</td>"; ?>
with
<?php echo "<td> View</td>"; ?>
You are having a ' after ?Id= which was creating the problem..
Instead of concatenating the $ID you could directly use
<?php echo "<td> <a href='preview.php?Id=$Id'>View</a></td>"; ?>
As in the docs
Double quote strings will display a host of escaped characters
(including some regexes), and variables in the strings will be
evaluated. An important point here is that you can use curly braces to
isolate the name of the variable you want evaluated. For example let's
say you have the variable $type and you what to echo "The $types are"
That will look for the variable $types. To get around this use echo
"The {$type}s are" You can put the left brace before or after the
dollar sign. Take a look at string parsing to see how to use array
variables and such
Please see this answer
Solving your problem:
<?php echo "<td> <a href='preview.php?Id=".$Id . "'>View</a></td>"; ?>
Alternatively you may define before using it:
<?php $link = "preview.php?Id=$Id"; ?>
<?php echo "<a href='$link'>View</a>"; ?>
Also you may try PHP in HTML
<a href='<?php echo "preview.php?Id=$Id"?>'>View</a>
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.
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 } ?>
What I am trying to do is change the image when an item is selected from the drop down. This is part of a form so I cant have the value change. However the option value is the row id, that row would also contain the target for the image. But because the target 'file' is called outside the loop it isn't firing.
I read I have to call it within the loop first but can't get it to work. Could you look at the code below and throw me a hint?
Thanks
<?php
include ("conned-db.php");
$result = mysql_query("SELECT * FROM gallery")
or die(mysql_error());
echo "<select id='gallery_id' name='gallery_id' style='width:200px;' >";
while($row = mysql_fetch_array( $result ))
{
echo '<option value=' . $row['id'] . '>';
echo $row['gallery_name'];
echo '</option/>';
}
echo "</select>";
echo "</td>";
echo "<td colspan='2' rowspan='2'>";
echo '<img src=' .$row['file']. '/></td>';
?>
Try this, I think this is what you are looking for
If you want to do something like this you must use Ajax. Here you go for the link that helps you to understand about Ajax.
http://www.w3schools.com/php/php_ajax_database.asp
Note:
If you wanted it to be only PHP without Javascript, you would have to sacrifice the 'must not refresh' constraint, as the only way to submit the form is by pressing the button, and submitting the content.
This should work too. If the file locations of the images are available at the time you load the page using ajax is not a must. You have to use ajax if you need to query the server again to retrieve the required file location. The following code assumes that you have the location of the images for each item of the dropdown list at the time you load the page.
<select id='gallery_id' name='gallery_id' style='width:200px;'
onchange='document.getElementById("image").src=this.options[this.selectedIndex].title' >
<?php
while($row = mysql_fetch_array( $result ))
{
?>
<option value='<?php echo $row["id"]; ?>' title='<?php echo $row["file"]; ?>'>
<?php echo $row["gallery_name"]; ?>
</option>
<?php
}
?>
<img id="image" />
Here is a short example which implements java scrip and php where i update src of an image based on the id from the select you might want to change with specific src based on that id
<?php
include ("conned-db.php");
$item = $_GET["imageid"];
if ($item == "")
{
$item = 1;
}
$result = mysql_query("SELECT * FROM gallery")
or die(mysql_error());
?>
<select id='gallery_id' name='gallery_id' onChange="window.location='file.php?imageid='+this.value" style='width:200px;' >
<?
while($row = mysql_fetch_array( $result ))
{
echo '<option value=' . $row['id'] . '>';
echo $row['gallery_name'];
echo '</option/>';
}
?>
</select>
</td>
<?
echo "<td colspan='2' rowspan='2'>";
?>
<img src=' <?=$item?> '/></td>
I have a post system in place
<?php
/**
Display the results from the database
**/
$q = ("SELECT * FROM threads ORDER BY posted");
$r = mysql_query($q);
if(mysql_num_rows($r)>0): //table is non-empty
while($row = mysql_fetch_assoc($r)):
$net_vote = $row['votes_up'] - $row['votes_down']; //this is the net result of voting up and voting down
?>
<div class='entry'>
<span class='link'>
<?php echo $row['author']; ?>
<?php $row['posted'] = date("jS M Y h:i",$row['posted']); echo $row['posted']; ?>
<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-text="<?php echo $row['message']; ?>">
Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
<br/>
<div class='message'><?php echo $row['message']; ?><br/></div>
<?php echo "<a href='msg.php?id=$row[id]'/> Comments/Add comments $row[replies]</a>" ?>
<?php echo "Likes: " . $row['votes_up'] . "  "; echo "Dislikes: " . $row['votes_down'] . " "; ?>
</span>
<span class='votes_count' id='votes_count<?php echo $row['id']; ?>'></span>
<span class='vote_buttons' id='vote_buttons<?php echo $row['id']; ?>'>
<a href='javascript:;' class='vote_up' id='<?php echo $row['id']; ?>'></a>
<a href='javascript:;' class='vote_down' id='<?php echo $row['id']; ?>'></a>
<br/>
</span>
</div>
<br/>
<?php
endwhile;
endif;
?>
I want to add text that says 'order by : Most recent | Most liked | least liked '
As you can see i think ive got it already posting most recent by defualt which is what i want.
But what i want also is when you click 'Most liked' & 'least liked' it sorts by 'Vote_up' ( likes) & 'vote_down' (dislikes) all on the same page and shows posts with most likes on them (most liked) and most dislike (least liked)
EDIT***
sorry my question is how can i add 2 functions that when on click sorts by 'most liked' and 'least liked'
in html:
<a href='script.php?order=recent'>Recent</a>
<a href='script.php?order=liked'>Liked</a>
...
in php:
if ($_GET['order'] == 'recent') $order = "posted";
elseif ($_GET['order'] == 'liked') $order = "smth";
...
$q = "SELECT * FROM threads ORDER BY ".$order."";
but actually it's better to use some js framework (i prefer extjs for that) to sort the output on client side
AFAIK you have few choices how to do this:
Have the links pass a $_GET value via URL (causes a page refresh!!), then test for this in the PHP and run the necessary SQL to get the new record order.
Use AJAX to perform the same request asynchronously, with a PHP script to handle the SQL function and return the desired results.
Use a jQuery plugin such as tablesorter to (probably) basically provide the same functionality as that in 2, or via it's own filtering system -- I'm not sure i've not used it!!
My preference would be 1. (i.e a pure PHP + MySQL solution) as this offers the best universal functionality. You could always add javascript / ajax later to make things more swish for more modern browsers and users!