Hey everyone,
I've got some code now that a friend of mine shot me and below is the index.php file and then below that is the voted.php file. It's not passing any of the +1 to anything that they voted for.
index.php
<?php
// Connects to your Database
$link = mysql_connect('localhost', 'user', 'pass');
mysql_select_db("database") or die(mysql_error());
?>
<?php include('header.php'); ?>
<div class="contentwide">
<div class="contentwrap">
<form method="post" action="voted.php">
<?php $query = "SELECT id, name, url FROM photos ORDER BY RAND() LIMIT 3"; $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { ?>
<input type="hidden" name="id" value="<?php {echo "{$row['id']}"; } ?>">
<div class="picturewrap">
<img src="<?php {echo "{$row['url']}"; } ?>" alt="<?php {echo "{$row['name']}"; } ?>" />
<div class="formwrapper">
<input type="radio" name="<?php {echo "images[{$row['id']}]";} ?>" value="1">
</div>
<div class="formwrapper">
<input type="radio" name="<?php {echo "images[{$row['id']}]";} ?>" value="2">
</div>
<div class="formwrapper">
<input type="radio" name="<?php {echo "images[{$row['id']}]";} ?>" value="3">
</div>
</div>
<?php } ?>
<div class="submitform">
<input type="image" src="images/button_submit.jpg" class="button_submit" value="Submit"><br/ >
</div>
</form>
</div>
</div>
<?php mysql_close($link); ?>
<?php include('footer.php'); ?>
voted.php
<?php
$images = $_POST['images'];
// Loop over each item in the $images array.
foreach ($images as $id => $vote) {
$query = "SELECT {$id} FROM photos";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
switch ($vote) {
case '1':
# Vote for a spoon
$spoon_votes = $row['spoon_votes'] + 1;
$update = "UPDATE photos SET spoon_votes={$spoon_votes} WHERE id={$row[id]}";
mysql_query($query);
break;
case '2':
# Vote for a fork
$fork_votes = $row['fork_votes'] + 1;
$update = "UPDATE photos SET fork_votes={$fork_votes} WHERE id={$row[id]}";
mysql_query($query);
break;
case '3':
# Vote for a knife
$knife_votes = $row['knife_votes'] + 1;
$update = "UPDATE photos SET knife_votes={$knife_votes} WHERE id={$row[id]}";
mysql_query($query);
break;
default:
die("Invalid value passed as a vote.");
break;
}
}?>
I set up the three columns so each row can have tallies for each item (spoon fork knife) this way I can keep track of how many times overall these pics get each vote.
The page will show three random pics every time it's loaded and the same pic might be in a different set. (ie: first page shows id's 1,4,9 while the next page load shows 5,9,13) So I need to tally each of the times the spoon fork or knife is voted for, for that pic.
Make sense? sorry if it sounds like rambling. I'm truly new to php like this. I work with wordpress and other blogging platforms and know my way around them, but writing codes is something completely new to me