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>
Related
I'm currently in the process of making a social network. I'm am displaying all registered users with the following code.
Is there a better way of doing this?
<?php
$query = $db->query("SELECT * from members");
while($r = $query->fetch()) {
echo '<div class="col-sm-6 col-md-6 col-lg-3 membersGrid">';
echo '<div class="profileThumb">';
echo '<img width="130" height="130" src="'.$r['profile_pic'].'"/><br>';
echo $r['username'], '<br>';
echo $r['country'], '<br>';
echo '<a class="viewProfile" href="profile.php"><button>View Profile</button></a>';
echo '</div>';
echo '</div>';
}
?>
I'm trying to create a link to the individual profile pages for each users. Currently each link is within the above while loop, however, I'm a little uncertain of how to link to the relevant profile page. I'm guessing I have to append a variable to the URL. Any guidance would be greatly appreciated.
echo '<a class="viewProfile" href="profile.php"><button>View Profile</button></a>';
It would depend on how your profile.php page is handling the GET variable that determines the profile of the person you are showing. Let's say that the variable is called id and that you have a row in your members table also called id (which acts as the unique key), then your anchor tag would look like:
echo '<a class="viewProfile" href="profile.php?id=' . $r['id']. '"><button>View Profile</button></a>';
First retrieve the user_id of the user from the database as you are doing with the query. Then give this id to the profile link as:
echo '<a href="profile.php?userid=' . $user_id . '>Linkname</a>';
Then in profile.php get this variable through:
$id = $_GET['userid'];
This way you can show the relevant user's profile in the profile.php.
Hope you might get the idea to work on.
If you are producing a list of links to user profiles then there are a couple of different options:
You can either append a $_GET variable to the end of your link as previously mentioned by Lloyd
echo '<a href="profile.php?if=' . $r['id'] . '">';
or you can send a $_POST variable; the easiest way to do this would be to create a list of forms:
<?php
$query = $db->query("SELECT * from members");
while($r = $query->fetch()) {
echo '<div class="col-sm-6 col-md-6 col-lg-3 membersGrid">';
echo '<div class="profileThumb">';
echo '<img width="130" height="130" src="'.$r['profile_pic'].'"/><br>';
echo $r['username'], '<br>';
echo $r['country'], '<br>';
echo '<form action="profile.php" method="post">';
echo '<input type="hidden" value="' . $r['id'] . '" />';
echo '<input type="submit" value="View Profile" />';
echo '</form>';
echo '</div>';
echo '</div>';
}
?>
Sorry if I've misread your question, hope this helps.
while ($r1 = $result->fetch_assoc()) {
echo "<tr>
<td>
<a name='".$r1["case_id"]."'
id='".$r1["case_id"]."'
href='modify.php'>".
$r1["case_id"].
"</a></td>
// code continues...
}
Multiple anchor tags will be created at run time and unique name and id will be assigned to each of them.
I want to know id or name of the anchor which was clicked to run modify.php.
You can do this -
<td><a name='".$r1["case_id"]."' id='".$r1["case_id"]."' href='modify.php?id=".$r1["case_id"]."'>".$r1["case_id"]."</a></td>
And when clicked get that with php
if(!empty($_GET['id'])) {
echo $_GET['id'];
}
<td><a name="<?php echo $r1['case_id'];?>" id="<?php echo $r1['case_id']?>" href='modify.php?id=<?php echo $r1["case_id"]?>'><?php echo $r1['case_id']?></a></td>
send id using query string and get the is in php page using GET method
<?php if(!empty($_GET['id'])) {
echo $_GET['id'];
}?>
By compiling this, display three image get my database and show it, when i click each image popup window show last image of my database.I want to know how to display the particular image, when i click first image,then popup window show first image and description, as well as same way second and third image. check to array loop in this code...
enter code here
<?php
$sql=mysql_query("select * from product_reg")or die(mysql_error());
while($row=mysql_fetch_array($sql))
{
$productname=$row['productname'];
$productid=$row['productid'];
$description=$row['description'];
$image=$row['image'];
$firstN = array();
$i=0;
$firstN = '<img src="'.$row ['image'].'">';
echo ' <a href="#" class="big-link" data-reveal-id="myModal" name="image" style="float:left;margin:100px 0 100px 100px;"> ' ; echo $productname;echo $firstN[$i];
$r=$firstN[$i];
echo '</a>';
$i++;
}
?>
<div id="myModal" class="reveal-modal">
<form>
<table>
<tr><td><?php echo $r; ?></td>
<td><h1>Reveal Modal Goodness</h1>
<p>This is a default modal in all its glory, but any of the styles here can easily be changed in the CSS.</p></td>
<a class="close-reveal-modal">×</a>
</div>
</body>
</html>
I guess you have the images stored in the database like BLOB data. If so you need to create a handler to retrieve those images and render them as image/[mime]...
So in short.
In your code you need to create a new file iz get_image.php in it you need to make a request to the server and retrieve the image so you can send it to the client.
In your code you need to change the image path to the handler path with some query parameters.
$firstN = '<img src="get_image.php?imageid='.$row ['productid'].'">';
There are a lot information how to render the image to the client from the internet.
may be you have to declare your $firstN = array(); and then incrementor $i=0; out of while loop and put in array like this:
$firstN[$i] = '<img src="'.$row['image'].'">';
below is the full code:
<?php
$sql=mysql_query("select * from product_reg")or die(mysql_error());
$firstN = array();
$i=0;
while($row=mysql_fetch_array($sql))
{
$productname=$row['productname'];
$productid=$row['productid'];
$description=$row['description'];
$image=$row['image'];
$firstN[$i] = '<img src="'.$row['image'].'">';
echo '<a href="#" class="big-link" data-reveal-id="myModal" name="image" style="float:left;margin:100px 0 100px 100px;"> ';
echo $productname;
echo $firstN[$i];
$r=$firstN[$i];
echo '</a>';
$i++;
}
?>
Updates:
You have a space here:
$firstN[$i] = '<img src="'.$row['image'].'">';
//-----------------------------^----here at this code block;
To get the last image , you need to modify your query to have SORT BY productid DESC
To display the images
echo "<a href='xxx.php?imgid=$image'><img src='yourimagesfolderpath/$image.jpg'> </a>";
to navigate in the images , you have to use JQUERY
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.
Basically, I am doing this: I have already read the database and obtained all the items, each includes a 'name' and a 'link'. On the page, I have an image, a select menu, and a button; each select option displays an item name (but no link information). I want to implement this: first select an option (nothing happen), and then click the button to change the image src to the selected item's link (in PHP variable). But I have trouble to pass the link to the javascript function, because it involves PHP variable. The code is like this:
<script type="text/javascript">
function selectItem(i) {
var name, link;
/*
...
*/
document.getElementById("name").innerHTML = name;
document.getElementById("pic").src = link;
}
</script>
...
<body>
<img id="pic" src="default.png">
<div id="name"></div>
<div onclick="location.href='javascript:selectItem(document.
getElementsByName("menu")[0].value)';"></div>
<select name="menu">
<option value="">select one</option>
<?php
$i = 0;
foreach ($items as $item):
echo ("<option value=\"" . $i . "\">" . $item['name'] . "</option>");
$i++;
endforeach;
?>
</select>
</body>
Thanks for helping!
Is the foreach loop actually looping? Try:
<?php
$i = 0;
foreach ($items as $item) { ?>
<option value="<? echo $i ?>"><? echo $item['name'] ?></option>
<? $i++; }
?>
I know this is late, but my solution is: to record name and link together (separated by " | ") in the attribute "value" of each option. Then there is no need to pass PHP variable to the JavaScript.