Okay, I think what I've got is pretty simple for an experienced web dev person. Right now, this this is the index page for State of Debate, my debate website. From the code below, you can see that I have a while loop to echo out each debate, which corresponds to a "did" in the mySQL database. What I need to do, for upvote/downvote purposes, is to get each "did" that goes through the loop. I would then pass it to the "upvote.php" and "downvote.php".
I've tried a foreach loop, although that could be the answer and I just didn't find it. I've used $_SESSION['did'] = $row['did'] and all that does is pass the last did to the PHP page.
Any help is appreciated. And please ask if you need more info.
<?php
$result = mysql_query("SELECT did,debatetitle FROM debates");
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td><a href='/debatepage.php?did=" . $row['did'] . " '>" . $row['debatetitle'] . "</a></td>";
?>
<tr><td>
<a href="upvote.php" onClick="alert('You have given this item a thumbs up! You can change your vote, or leave it how it is.')"; onMouseOver="return changImage()" onMouseOut= "return changImageBack()" ><img
name="jsbutton2" src="Graphics/thumbs-up-unclicked.jpeg" width="75" height="75" border="0"
alt="javascript button"></a>
<script type="text/javascript">
function changImage()
{
document.images["jsbutton2"].src= "Graphics/thumbs-up.jpeg";
return true;
}
function changImageBack()
{
document.images["jsbutton2"].src = "Graphics/thumbs-up-unclicked.jpeg";
return true;
}
</script>
<a href="downvote.php" onClick="alert('You have given this item a thumbs down! You can change your vote, or leave it how it is.')"; onMouseOver="return changeImage()" onMouseOut= "return changeImageBack()" ><img
name="jsbutton1" src="Graphics/thumbs-down-unclicked.jpeg" width="75" height="75" border="0"
alt="javascript button"></a>
<script type="text/javascript">
function changeImage()
{
document.images["jsbutton1"].src= "Graphics/thumbs-down.jpeg";
return true;
}
function changeImageBack()
{
document.images["jsbutton1"].src = "Graphics/thumbs-down-unclicked.jpeg";
return true;
}
</script>
</tr></td>
<?php
echo "</tr>";
}
echo "</table>";
change this:
<a href="upvote.php" onCli....
to
<a href="upvote.php?did<?PHP echo $row['did']"; ?> onCli
and the same with downvote link.
To use the variable in upvote.php:
<?PHP
echo $_GET['did'];
?>
Related
I am trying to output a key value from JSON data and want to check for multiple keys.
I want to display the title of a movie and or tv show.
if($channel=="multi" AND !empty($movies)){
foreach ($movies as $movie) {
if(!empty($movie['poster_path'])){
echo '<div class="mov">
<a href="'.$movie['media_type'].'/'.$movie['id'].'">
<img src="https://image.tmdb.org/t/p/w185'.$movie['poster_path'].'">
<h2 class="title">'.$movie['title'].'</h2>
</a>
</div>';
}
}
}
whereas <h2 class="title">'.$movie['title'].'</h2> also needs to check for <h2 class="title">'.$movie['name'].'</h2>
How can I check for both 'title' and 'name' in the same go?
I am pretty new to all this and learning on the go. So please correct me as well if there are any mistakes in my coding!
If you're trying to get $movie['title'] or $movie['name'] (because you're unsure what the variable will be called) you can use an if statement. (This is not a pretty way of writing it -- but it is the clearest way).
if($channel=="multi" AND !empty($movies)){
foreach ($movies as $movie) {
if(!empty($movie['poster_path'])){
echo '<div class="mov">
<a href="'.$movie['media_type'].'/'.$movie['id'].'">
<img src="https://image.tmdb.org/t/p/w185'.$movie['poster_path'].'">
<h2 class="title">';
//AN IF STATEMENT TO CHOOSE EITHER TITLE OR MOVIE
if(!empty($movie['title']) {
echo $movie['title'];
} else {
echo $movie['name'];
}
echo .'</h2>
</a>
</div>';
}
}
}
Alternatively, you could also make $movie['title'] equal to $movie['name'] if it is not blank.
if($channel=="multi" AND !empty($movies)){
foreach ($movies as $movie) {
if(!empty($movie['poster_path'])){
// MAKE MOVIE TITLE = MOVIE NAME IF IT IS NOT BLANK
if(!empty($movie['name'])){
$movie['title'] = $movie['name'];
}
echo '<div class="mov">
<a href="'.$movie['media_type'].'/'.$movie['id'].'">
<img src="https://image.tmdb.org/t/p/w185'.$movie['poster_path'].'">
<h2 class="title">'.
$movie['title'].
'</h2>
</a>
</div>';
}
}
}
There are usually many ways to solve a problem. Think through the logic. If this than that. If not this, than that instead.
I'm trying to show an image (or rather a link to an image) stored in a database and I'd like to get the image to show only if the link is set in the database.
Currently, if the link is not set (value is null), it shows a broken link.
Is there a way to for example use an if-statement and echo a HTML-code?
Something like this:
(The value have been fecthed to array $current in this example:)
<?php
if(isset($current['image']) {
echo "<img src='<?php echo $current['image'];
?>' class='left' style='max-height:20em; max-width:15em; margin-right:1em; margin-top:0;'})">
You used <?php twice, you have problem with quotes, brackets, etc.
<?php
if (!empty($current['image'])) {
echo "<img src='" . $current['image'] . "' class='left' style='max-height:20em; max-width:15em; margin-right:1em; margin-top:0;'>";
} else {
// here you can write for example default no-image image or whatever yo want, if you want
}
Nevermind, got it.
-Solution:
<?php if(isset($current['image'])): ?><img src="<?php echo $current['image']; ?>" class="left" style="max-height:20em; max-width:15em; margin-right:1em; margin-top:0;})">
<?php endif; ?>
<?php
if(isset($current['image'])) {
?>
<img src='<?php echo $current['image'];?>' class='left' style='max-height:20em; max-width:15em;
margin-right:1em; margin-top:0;'>
<?php
}
?>
I have a set of image url's in a database which I am echoing into an <img>tag to display on a website.
I am using bootstrap and have my basic model set up like this:
<div class="row-fluid gallery">
<div class="span3">
<img src="some fancy url here for the image">
</div>
</div>
Now if you have ever used bootstrap you know once that span3 reaches 12 (basically when 4 images are displayed in the row). You must start the above all over to keep the images all in line.
I have a PHP script below that echoes out the image source and the above layout. The problem is, I have more than 4 images to echo out. I removed credentials for security purposes.
$con=mysqli_connect("localhost","username","password","db_name");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM gallery");
while($row = mysqli_fetch_array($result))
{
echo "<div class='row-fluid gallery'>";
echo "<div class='span3'>";
echo"<img src='". row['image_url']."'>";
echo "</div>";
echo "</div>";
}
mysqli_close($con);
My question is how do you do something like:
for every 4th image {
<div class="row-fluid gallery">
<div class="span3">
<img src="some fancy url here for the image">
</div>
</div>
}
Basically, I can say it in English and know what I need. But I can't say it in PHP and have it know what I need. Any help would be appreciated.
Create a counter variable outside the loop and check every fourth
$i = 1;
while($row = mysqli_fetch_array($result))
{
// do every time
if($i % 4 == 0)
{
// do only each 4th time
}
$i++;
}
$counter=0;
while($row = mysqli_fetch_array($result))
{
echo "<div class='row-fluid gallery'>";
echo "<div class='span3'>";
echo"<img src='". row['image_url']."'>";
echo "</div>";
echo "</div>";
$counter++;
if (!($counter%4))
// do your fancy staff, this is the forth image
}
I'd like to show the first 5 names in a list and toggle the display of any additional names as a single block.
I've currently got the names list as an array object though I'm happy to change it to an array if the solution would be simpler with that.
Here's what I have so far which is *in*complete because I don't know how to create the hidden div of names:
PHP
$names_count=0;
echo '<div id='nameList' class='toggler'>';
foreach($names as $name){
echo '<a id='name'.$name->acct_id.'>'.$name->full_name.'</a>';
if($names_count<=4){
echo '</div><!--toggler div-->';
}
else
<div class='namesList' style='display:none'>
//put additional names in hidden div?
</div>
}
$names_count++;
} //endforeach
JS:
UPDATE Sorry for the confusion. This isn't really a javascript question so I deleted that tag but I'm including the following jQuery code snippet for completeness with the PHP
$('.toggler').click(function(){
var id=this.id;
$('#'+id).toggle();
});
PHP
$names_count = 0;
echo '<div id="nameList" class="toggler">';
foreach($names as $name) {
echo '<a id="name' . $name->acct_id . '">' . $name->full_name . '</a>';
if ($names_count == 4) {
echo '</div><div class="hidden">';
}
$names_count++;
}
echo '</div>';
JS
$('.toggler').click(function(){
$(this).next().toggle();
});
CSS
.hidden {
display: none;
}
Here's an example with two while loops.
$names = array('Bob', 'Andy', 'Tim', 'Max', 'Roger', 'John', 'Test');
$nameCount = count($names);
$nameIndex = 0;
echo '<div id="nameList" class="toggler">';
// Show the first 5 names.
while ($nameIndex < min(5, $nameCount)) {
$name = $names[$nameIndex++];
echo '<a id="name' . $name . '">' . $name . '</a>';
}
// Show the remaining names in a hidden div.
if ($nameIndex < $nameCount)
{
echo '<div class="hiddenNames" style="display:none">';
while ($nameIndex < $nameCount) {
$name = $names[$nameIndex++];
echo '<a id="name' . $name . '">' . $name . '</a>';
}
echo '</div>';
}
echo "</div>";
That code produces the following output.
<div id="nameList" class="toggler">
<a id="nameBob">Bob</a>
<a id="nameAndy">Andy</a>
<a id="nameTim">Tim</a>
<a id="nameMax">Max</a>
<a id="nameRoger">Roger</a>
<div class="hiddenNames" style="display:none">
<a id="nameJohn">John</a>
<a id="nameTest">Test</a>
</div>
</div>
It also safe if you have less than 5 names; the script would produce :
<div id="nameList" class="toggler">
<a id="nameBob">Bob</a>
<a id="nameAndy">Andy</a>
<a id="nameTim">Tim</a>
</div>
For the JS, I would probably do something along the lines of :
$('.toggler').click(function(){
$('.hiddenNames').toggle();
});
Even if the code is a bigger, I find it easier to follow and probably easier to maintain in the long run. (Opinion)
Hope this helps!
To make a <div> hidden:
<div style="display: hidden"></div>
Then the jQuery should make it visible with the .toggle() command.
if(!isset($_POST['JoinFaction'])) {
echo '<form method="post" action="'.$page_name.'"><dl id="sample" class="dropdown" align="left">'
.'<dt><a href="#"><span>Recruiting Factions ['.$numrows.']</span>'
.'</a></dt><dd><ul>';
while($faction_re = mysql_fetch_array($sql)){
echo '<li><a href="#">'.$faction_re['f_name'].''
.'<span class="value">'.$faction_re['f_id'].'</span></a></li>';
}
echo '</ul></dd></dl></td>'
.'<td class="faction_bgcolour" align="center"><input type="image" '
.'name="JoinFaction" value="JoinFaction" width="54" height="45" '
.'src="images/global/game/faction/join_faction.png" /></td></form>';
//get output as a variable to use in later code
echo $selected_faction = '<span id="result"></span>';
} else { $selected_faction = '<span id="result"></span>';
echo '<div class="faction_text">Joined: '.$selected_faction.'.</div>';
}
when I:
echo $selected_faction;
in the if statement, it gets the right faction value (i'm using custom jquery drop down boxes by the way here).
The problem is that this id is not being passed to the else statement where i can update the database. I'm really not sure how I would pass the id on to the else statement.
echo $selected_faction is blank in the else clause
Any help would be greatly appreciated.
You're going to need to send the content of that div back to your server with AJAX.
You could start learning here: http://api.jquery.com/category/ajax/
I suppose a good starting point might be: http://api.jquery.com/jQuery.post/