DanielHardy Posted July 29, 2011 Share Posted July 29, 2011 Hi All, I am dynamically producing output of submit buttons like this: echo '<div id="downloads" style="font-family:Arial;width:900px;height:30px;float:left;margin-bottom:5px;background-color:#069e0d;line-height:30px;color:#ffffff;"><div id="filename" style="float:left;padding-left:20px;width:600px;height:30px;border-right:5px solid #ffffff;">'.$name.'</div><div id="date" style="text-align:center;font-size:11px;float:left;width:125px;border-right:5px solid #ffffff;height:30px;">'.$date.'</div><div id="date" style="float:left;width:140px;text-align:center;font-size:11px;height:30px;"><input type="submit" name="admin[]" value="'.$id.'" style="height:30px;width:145px;border:none;background-image:url(download.png);color:transparent;background-color:#069e0d;"></div></div>'."\n"; } I need to somehow reproduce this, but use a href instead of the button. The "admin" value is used by the following piece of code: <?php mysql_connect("*******", "*******", "*****") or die(mysql_error()); mysql_select_db("***********") or die(mysql_error()); $sql = "SELECT id,files,name,date,status,user FROM user1_files ORDER by id DESC"; $cookie = $_COOKIE['ID_my_site']; $result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR); while(list($id,$files,$name,$date,$status,$user)=mysql_fetch_row($result)){ $updated = FALSE; if(count($_POST) > 0){ $admin = $_POST['admin']; array_map('intval',$admin); $admin = implode(',',$admin); mysql_query("UPDATE user1_files SET status=1 WHERE id IN ($admin)") or trigger_error(mysql_error(),E_USER_ERROR); $updated=TRUE; header("Location: $files"); } } ?> All that does is update a value from 0 to 1 when the button has been clicked. I want the same functionality but it does not seem to work when I replace the button with an image like follows: echo '<div id="downloads" style="font-family:Arial;width:900px;height:30px;float:left;margin-bottom:5px;background-color:#069e0d;line-height:30px;color:#ffffff;"><div id="filename" style="float:left;padding-left:20px;width:600px;height:30px;border-right:5px solid #ffffff;">'.$name.'</div><div id="date" style="text-align:center;font-size:11px;float:left;width:125px;border-right:5px solid #ffffff;height:30px;">'.$date.'</div><div id="date" style="float:left;width:140px;text-align:center;font-size:11px;height:30px;"><a href="' .$files . '" name="admin[]" value="'.$id.'" style="height:30px;width:145px;border:none;background-image:url(download.png);color:transparent;background-color:#069e0d;"></a></div></div>'."\n"; } Hope that all makes sense Thanks in advance Dan Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 29, 2011 Share Posted July 29, 2011 the <a href>'s should work fine, but you need to add the variables to the url string. imagine you link points to 'page2.php', you will have to ad whatever variables and values you want, so instead of something like this <a href="page2.php">bla bla bla</a> you'll have: <a href="page2.php?var1=hello&var2=world">bla bla bla</a> then, to grab the variables, instead of $_POST, use $_GET (Mind you, sending variables like this through the url string, when they will affect your database values is quite insecure and can lead to problems. It would be fine if on the next page you were just SELECT(ing) stuff from the database, but you're also changing it with an UPDATE statement.) Quote Link to comment Share on other sites More sharing options...
DanielHardy Posted July 29, 2011 Author Share Posted July 29, 2011 Thanks for your reply. I'm not sure that will help me. The redirect redirects to a database based URL. Let me explain. if($status==1){ echo '<div id="downloads" style="font-family:Arial;width:900px;height:30px;float:left;margin-bottom:5px;background-color:#cccccc;line-height:30px;"><div id="filename" style="float:left;padding-left:20px;width:600px;height:30px;border-right:5px solid #ffffff;">'.$name.'</div><div id="date" style="text-align:center;font-size:11px;float:left;width:125px;border-right:5px solid #ffffff;height:30px;">'.$date.'</div><div id="date" style="float:left;width:140px;text-align:center;font-size:11px;height:30px;"><a class="downloads" href="' . $files . '" title="Your File"><img src="viewed.png"></a></div></div>'."\n"; } The above code (for each) displays the name, date and URL of all database table objects where status = 1. This all displays fine and the "$files" variable does indeed link to where it should. However, When status equals 0, I am using the following code: $sql = "SELECT id,files,name,date,status,user FROM user1_files ORDER by id DESC"; $cookie = $_COOKIE['ID_my_site']; $result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR); if($status==0){ while(list($id,$files,$name,$date,$status,$user)=mysql_fetch_row($result)){ $updated = FALSE; if(count($_POST) > 0){ $admin = $_POST['admin']; array_map('intval',$admin); $admin = implode(',',$admin); mysql_query("UPDATE user1_files SET status=1 WHERE id IN ($admin)") or trigger_error(mysql_error(),E_USER_ERROR); $updated=TRUE; echo "<meta http-equiv='refresh' content='1;URL=". $files ." ' />"; } } } to update the status to 1 when the button is clicked. However, the files variable for all objects is automatically redirecting to the id with the lowest id. I.e I want to link to the url associated with 6, it ill link to URL associated with 1. Thanks again Dan Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 29, 2011 Share Posted July 29, 2011 Basically what you're saying is that the id in $files is wrong in the second case but correct in the first ??? You can just add the new id to it with a different name. Something like: $files .= "&id2=".$id; Quote Link to comment Share on other sites More sharing options...
DanielHardy Posted July 29, 2011 Author Share Posted July 29, 2011 No, The id is fine, and works as it should. In short, it's picking up the right ID, but not the right $files. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted July 29, 2011 Share Posted July 29, 2011 what is in $files? can you post an example please (from here I can only guess) Quote Link to comment Share on other sites More sharing options...
DanielHardy Posted July 29, 2011 Author Share Posted July 29, 2011 Sorry, Files contains the link to a file name. I's confusing as it is picking up the correct filename, date and id, but not linking to the correct path. [EDIT] files contains the link to a file path, not name [/EDIT] Quote Link to comment Share on other sites More sharing options...
DanielHardy Posted July 29, 2011 Author Share Posted July 29, 2011 SOLVED, Was simply a case of specifying to the update function only to carry out the actions when the id is equal to one in the database. I.e Instead of $sql = "SELECT id,files,name,date,status,user FROM user1_files ORDER by id DESC"; $cookie = $_COOKIE['ID_my_site']; $result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR); if($status==0){ while(list($id,$files,$name,$date,$status,$user)=mysql_fetch_row($result)){ $updated = FALSE; if(count($_POST) > 0){ $admin = $_POST['admin']; array_map('intval',$admin); $admin = implode(',',$admin); mysql_query("UPDATE user1_files SET status=1 WHERE id IN ($admin)") or trigger_error(mysql_error(),E_USER_ERROR); $updated=TRUE; echo "<meta http-equiv='refresh' content='1;URL=". $files ." ' />"; } } } I used if(count($_POST) > 0){ $admin = $_POST['admin']; array_map('intval',$admin); $admin = implode(',',$admin); $sql = "SELECT id,files,name,date,status,user FROM user1_files WHERE id IN ($admin)"; $cookie = $_COOKIE['ID_my_site']; $result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR); if($status==0){ while(list($id,$files,$name,$date,$status,$user)=mysql_fetch_row($result)){ $updated = FALSE; mysql_query("UPDATE user1_files SET status=1 WHERE id IN ($admin)") or trigger_error(mysql_error(),E_USER_ERROR); $updated=TRUE; echo "<meta http-equiv='refresh' content='1;URL=". $files ." ' />"; } } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.