PHP - Get The Id Of Current Table With Buttons In While Loop For Rating System
I've built a database based rating system, and till now everything works as expected except of one minor thing that breaks the while rating system.
When you press thumbs up or down it's only adding the vote to the LAST printed table in the while loop, it does NOT add it to the current table where the button was pressed. This is the while loop: <?php function knuffix_list ($query, $user_name, $avatar_path, $dbc) { $data = mysqli_query ($dbc, $query) or die (mysqli_error ($dbc)); //Loop through the array of data while ($row = mysqli_fetch_array ($data)) { global $con_id; $con_id = $row['con_id']; echo "<table padding='0' margin='0' class='knuffixTable'>"; echo "<tr><td width='65px' height='64px' class='avatar_bg' rowspan='2' colpan='2'><img src='$avatar_path' alt='avatar' /></td>"; echo "<td class='knuffix_username'><strong>" . $user_name . " ___ "; echo "</strong><br />" . $row['category'] . " | " . date('M d, Y', strtotime($row['contributed_date'])) . "</td></tr><tr><td>"; echo "<form action='' method='post'> <button type='submit' name='plusVote' value='like'>Y</button> <button type='submit' name='minusVote' value='dislike'>N</button> <input type='hidden' name='hidden_con_id' value='<?= " . $con_id . " ?>' /> </form></td><td class='votes'>Y[ - ] | N[ - ]</td></tr>"; echo "<tr><td class='knuffix_name' colspan='3'><strong>" . htmlentities($row['name']) . "</strong><br /></td></tr>"; echo "<tr><td colspan='2' class='knuffix_contribution'><pre>" . $row['contribution'] . "</pre><br /></td></tr>"; echo "</table>"; } mysqli_close($dbc); } ?> The con_id is the value how I wanted to determine which table it is, when I echo $con_id out (inside the while loop) it's showcasing the numbers correctly, but it doesn't work with the buttons, the buttons simply correspond to the last printed table in the while loop. I think it's because the while loop goes through each row until it's finished and it stops. This is the rating script: // RATING SYSTEM $plus_vote = $_POST['plusVote']; $minus_vote = $_POST['minusVote']; if ($plus_vote || $minus_vote) { $query = "SELECT * FROM con WHERE con_id = '$con_id'"; $query = mysqli_query ($dbc, $query) or die (mysqli_error($dbc)); $assoc_vote = mysqli_fetch_assoc ($query); if ($plus_vote) { $vote_up = $assoc_vote['likes'] + 1; $query = "UPDATE con SET likes = '$vote_up' WHERE con_id = '$con_id'"; $query_run = mysqli_query ($dbc, $query) or die (mysqli_error ($dbc)); mysqli_close($dbc); echo "test " . $con_id; } elseif ($minus_vote) { $vote_down = $assoc_vote['dislikes'] - 1; $query = "UPDATE con SET dislikes = '$vote_down' WHERE con_id = '$con_id'"; $query_run = mysqli_query ($dbc, $query) or die (mysqli_error ($dbc)); mysqli_close($dbc); echo "test " . $con_id; } } If I would be able to make the buttons in the while loop to RECOGNIZE the correct con_id of the table the button was being pressed then the script would work as intended and it would add the vote to the correct row in the table. How could I solve this problem, because I really have no idea? Similar TutorialsI have two sets of codes that work perfectly. One is for the table and the other is for the rating system. I can't seem to figure out how to combine the two codes so that the rating system is displayed INSIDE the table instead of after it. I'm not sure if it's possible, but I've seen it used on sites like Netflix. If you can tell me how to fix it or give me a different suggestion I would be eternally thankful. What I am trying to do ultimately is have a table that has a star rating associated with each entry without linking to a full page. Code: [Select] <link rel="stylesheet" type="text/css" href="style.css"> <script type="text/javascript" src="/jquery_star.js"></script> <script type="text/javascript" src="/script.js"></script> <?php $con = mysql_connect("localhost","myhowd5_undrdg","indie500"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("myhowd5_jo151", $con); $result = mysql_query("SELECT * FROM jos_easytables_table_data_1"); echo "<table border='1'> <tr> <th>Artist</th> <th>Genre</th> <th>Your Rating</th> <th>Average Rating</th> <th>Total Votes</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['column0'] . "</td>"; echo "<td>" . $row['column1'] . "</td>"; echo "<td>" . $row['rating'] . "</td>"; echo "<td>" . $row['total_rating'] . "</td>"; echo "<td>" . $row['total_ratings'] . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($con); ?> <?php mysql_connect("localhost", "myhowd5_undrdg", "indie500") or die(mysql_error()); mysql_select_db("myhowd5_jo151") or die(mysql_error()); $query = mysql_query("SELECT * FROM jos_easytables_table_data_1"); while($row = mysql_fetch_array($query)) { $rating = (int)$row[rating]; ?> <div class="floatleft"> <div id="rating_<?php echo $row[id]; ?>"> <span class="star_1"><img src="/star_blank.png" alt="" <?php if($rating > 0) { echo"class='hover'"; } ?> /></span> <span class="star_2"><img src="/star_blank.png" alt="" <?php if($rating > 1.5) { echo"class='hover'"; } ?> /></span> <span class="star_3"><img src="/star_blank.png" alt="" <?php if($rating > 2.5) { echo"class='hover'"; } ?> /></span> <span class="star_4"><img src="/star_blank.png" alt="" <?php if($rating > 3.5) { echo"class='hover'"; } ?> /></span> <span class="star_5"><img src="/star_blank.png" alt="" <?php if($rating > 4.5) { echo"class='hover'"; } ?> /></span> </div> </div> <div class="star_rating"> (Rated <strong><?php echo $rating; ?></strong> Stars) </div> <div class="clearleft"> </div> <?php } ?> I'm making a game where you earn points and lose points based on the actions that you take and the actions that your opponents take. What's the best way to create this system. Should I use mysql to store all the data and if so, should I set up the actions in a seperate table or should I just join the actions from all of my other tables. I'm not quite sure what the best method is. Hi All I am using the following method to check if user is logged in to a member area. However, I ahve a password change section that then redirects back the the password change page within the logged in area. After password changes it automatically redirects back to the index page rather than password page and comes up with a redirect error (too many redirects) How can i change the code to match the new change in password. I think its a sessions problem but not great with sessions! Code: [Select] if(isset($_COOKIE['ID_my_site'])) { $username = $_COOKIE['ID_my_site']; $pass = $_COOKIE['Key_my_site']; $check = mysql_query("SELECT * FROM member_users WHERE email = '$username'")or die(mysql_error()); while($info = mysql_fetch_array( $check )) { $usernameid=$info['username']; $userid = $info['id']; $firstname = $info['fname']; $lastname = $info['lname']; //if the cookie has the wrong password, they are taken to the login page if ($pass != $info['password']) { header("Location: index.php"); } //otherwise they are shown the admin area else { echo "<div class=\"memberloginactive\">Welcome $firstname | Your Profile | <a href=\"edit-profile.php?id=$userid\">Edit Profile</a> | <a href=\"logout.php\">Logout</a></div><div class=\"clear\"></div>"; } } } else { if I'm outputting from mysql with this: Code: [Select] // Connects to your Database mysql_connect("localhost", "root", "") or die(mysql_error()) ; mysql_select_db("test") or die(mysql_error()) ; //Retrieves data from MySQL $data = mysql_query("SELECT * FROM tickets") or die(mysql_error()); //Puts it into an array while($info = mysql_fetch_array( $data )) { //Outputs the image and other data Echo "<b>asset number:</b> ".$info['AssetNumber'] . "<br> "; Echo "<b>subject:</b> ".$info['subject'] . " <br>"; Echo "<b>notes:</b> ".$info['Notes'] . " <hr>"; } can I base the record it outputs somehow on this: Code: [Select] $value=""; $value = ProcessLargeText(GetData($data,"Asset Number", ""),"","",MODE_VIEW); if($mainTableOwnerID=="Asset Number") $ownerIdValue=$value; $xt->assign("Asset_Number_value",$value); if(!$pageObject->isAppearOnTabs("Asset Number")) $xt->assign("Asset_Number_fieldblock",true); else $xt->assign("Asset_Number_tabfieldblock",true); It's kind of like a job history so its getting records of job history from one table while the second piece of code is getting the asset details from a main table Hi
I am very new to PHP & Mysql.
I am trying to insert values into two tables at the same time. One table will insert a single row and the other table will insert multiple records based on user insertion.
Everything is working well, but in my second table, 1st Table ID simply insert one time and rest of the values are inserting from 2nd table itself.
Now I want to insert the first table's ID Field value (auto-incrementing) to a specific column in the second table (only all last inserted rows).
Ripon.
Below is my Code:
<?php $con = mysql_connect("localhost","root","aaa"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("ccc", $con); $PI_No = $_POST['PI_No']; $PO_No = $_POST['PO_No']; $qry = "INSERT INTO wm_order_entry ( Order_No, PI_No, PO_No) VALUES( NULL, '$PI_No', '$PO_No')"; $result = @mysql_query($qry); $val1=$_POST['Size']; $val2=$_POST['Style']; $val3=$_POST['Colour']; $val4=$_POST['Season_Code']; $val5=$_POST['Dept']; $val6=$_POST['Sub_Item']; $val7=$_POST['Item_Desc']; $val8=$_POST['UPC']; $val9=$_POST['Qty']; $N = count($val1); for($i=0; $i < $N; $i++) { $profile_query = "INSERT INTO order_entry(Size, Style, Colour, Season_Code, Dept, Sub_Item, Item_Desc, UPC, Qty, Order_No ) VALUES( '$val1[$i]','$val2[$i]','$val3[$i]','$val4[$i]','$val5[$i]','$val6[$i]','$val7[$i]','$val8[$i]','$val9[$i]',LAST_INSERT_ID())"; $t_query=mysql_query($profile_query); } header("location: WMView.php"); mysql_close($con); ?>Output is attached. i get this error Warning: current() [function.current]: Passed variable is not an array or object.. for this Code: [Select] $lastblock = current( ${"s".$row} ); print_r($lastblock); when i change to this it works.. Code: [Select] $lastblock = current( $s0 ); print_r($lastblock); The problem is i won't know the $row seeing as it is in a while loop. Solution? Hi, I'm wanting to find rows whose date is within the next week of the current month of the current year. The format of the date is, for example: 2010-10-28 Any ideas guys? Thanks lots! Hello - I've come to an issue with something I'm working on and have searched around with no luck. When editing user accounts I want it to not be able to change the e-mail address into existing ones on other rows, but when submitting the form it takes into account that this particular rows email address is the same as the one which was sent via $_POST and throws up the error. The desired behaviour I want is for it to ignore the ID of the row which was posted, but take into account every other row. Here's the code as it stands at the moment: $email = $_POST['email']; $checkemail = mysql_query("SELECT email FROM users WHERE email='$email'"); } if (mysql_num_rows($checkemail) > 0) { return $this->error("The e-mail address you provided is already associated with an account."); } How should I go about keeping track of photos that a user has rated, since I only want the user to rate the photo once? Can I store arrays in a mysql database? thanks, George I have a string for rating, containing numbers from 0-5. How I can assign an image to each range: 0-0.5 displaying 05.jpg 0.5-1.0 displaying 10.jpg . . . 4.5-5.0 displaying 50.jpg rating_bar($rows['id'],'6','static')." Show a star rating but the stars are to big. How can I change the size of the rating_bar, like I change the font size? echo "<TD style='font-size: 10px;' ALIGN=justify width=50% height=10 scope='row'><p>Please rate</p>". rating_bar($rows['id'],'6','static')."</TD>"; I need a php/mysql star rating which has 3 criteria to rate and at the bottom a link when clicked saves the ratings. Any idea of where to get that? Something like this Rate criteria 1 Rate criteria 2 Rate criteria 3 Save ratings for starters, i am a complete beginner at PHP, so i know almost nothing.
so, the basic outline is that i am required to create a table with 2 columns, and use PHP where i can
so the first set of code lays down the table and the ability to type in text of "name" and "surname". ----i think this "input" is HTML, yes?? is there a way to enter it as PHP?
<table border=\"2\"> <tr><th><b>Requirements</b></th><th><b>Selections</b></th></tr> <tr><td>Name</td><td><input type="text" name="name"/></td></tr> <tr><td>Surname</td><td><input type="text" name="surname"/></td></tr>so, for this next section, adding on is the part for "age". i want this to appear as a dropdown selection. i've written PHP where possible, however this does not work when opening the file in a browser. it simply leaves a blank dropdown menu with no options. ???? <tr><td>Age</td><td><select> <?php for ($num=11; $num<=22; $num++){ echo '<option>' .$num. '</option>'; } ?> </select>lastly, i have the part for "activity choice". this again i believe i wrote the radio buttons in HTML??? am i able to write this as PHP???? <tr><td>Activity Choice</td><td><input type="radio" name="activityChoice" value "music"/> Music ($30.00)<br> <input type="radio" name="activityChoice" value "swimming"/> Swimming ($25.50)<br> <input type="radio" name="activityChoice" value "tennis"/> Tennis ($20.00)<br> <input type="radio" name="activityChoice" value "basketball"/> Basketball ($15.50)<br> <input type="radio" name="activityChoice" value "netball"/> Netball ($15.50)<br> <input type="radio" name="activityChoice" value "dance"/> Dance ($10.50)<br> <input type="radio" name="activityChoice" value "communityService"/> Community Service (No Charge)</td></tr> Hi i have this drop down list current the year is 2010 and downwards but i want to change the list to 2010 upwards u can notice on the 50-- so shows current year minus so current is 2010 to 61 how can i change 2010 to 2030 or sunfin?? echo '<select name="year_of_birth">',"\n"; $year = date("Y"); for ($i = $year;$i > $year-50;$i--) { if($i == $thisYear) { $s = ' selected'; } else { $s=''; } echo '<option value="' ,$i, '"',$s,'>' ,$i, '</option>',"\n"; } echo '</select>',"\n"; Hi, When I use the below table in a while loop, it will repeat down the page. I want it to repeat in rows of 3, how can I do this? Code: [Select] while () { echo ' <table width="50%" cellpadding="2" cellspacing="1" bgcolor="'.$border.'"> <tr > <td colspan="5" height="20" align="center" class="title" bgcolor="'.$bghead.'"><b>'.$pt['platform'].'</b></td> </tr> </table>'; } Thanks! Using a for loop counting from 0 to 9, I need to make this into a table. Making sure the table is 100% of the page. Any sort of help is appreciated. Thank you 0 1 2 3 4 5 6 7 8 9 hello dear PHP-Fans - greetings to you - and a happy new year!! i set up a WAMP-System on my openSuse 11.4 system. In order to learn as much as i can bout PHP i want to do some tests and write some scripts. Well the WAMP is allready up and running. Now i try to give the writing access to the folder mkdir /srv/www/ where the php-scripts should go in... i want to give write permission to all to all files in /srv/www As root I generally: mkdir /srv/www/ chown <webmaster usrername> /srv/www/ /srv/www/ should be readable and traversable by all, but only writeable by it's owner (the user designated as the webmaster.) can i do this like mentioned above,... Love to hear from you greetings db1 The following generic code prints out a given table in my database, by first getting the fieldnames and putting them as the title of each column, and then getting the values. How do I change it so that if the name of the field is "password", the value will echo "----" instead of the password? $sql = "SELECT * FROM $tablename"; $result = mysql_query($sql) or die("Query failed : " . mysql_error()); $row = 1; ?> <table width="90%" border="1"><?php //the MYSQL_ASSOC gets field names instead of numbers while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { if ($row == 1) { ?><tr><?php foreach ($line as $col_key => $col_value) { ?><th><?php echo $col_key;?></th><?php } ?></tr><?php } ?><tr><?php foreach ($line as $col_value) { ?><td><?php echo $col_value;?></td><?php } ?></tr><?php $row++; I am trying to place a unique number into a mysql table. Currently, my code generates a random number, then is supposed to scan through the table for that number. If the code finds that number already in the table, it generates a new random number and repeats the process. I have commented my code for the purpose of this help forum: Code: [Select] $result = mysqli_query($link,"SELECT * FROM testTable"); do { $end = true; //prepares end of loop $idNum = rand(1,10); //rand(1,999999); <-- for testing purposes I have reduced the number generated $idNumTx = (string)$idNum; //loop through the rows while ($row = mysqli_fetch_assoc($result)) { if ($row['idNum'] = $idNum) //check if the random number equal to this row { $end = false; //prep end of loop repeat echo $idNumTx; //display rand number that failed for testing purposes echo " NO! "; //display error for testing purposes } } } while(!$end); I know I must be doing something wrong, as when I run this, it runs the if statement within the while loop always executes, and I get an output like: Code: [Select] 1 NO! 1 NO! 1 NO! 1 NO! 1 NO! 1 NO! 5 Win Win is when it places the value before it, in this case 5, into the table. However, the value of 5 might already be in the table and it doesn't seem to matter. I execute the code multiple times, and it seems to increase the number of "# NO!" almost (but not every) time. However, each time ALL of the "# NO!" are the same #, and the "# Win" just seems to be random (as it should be, but not unique). Checking the table after shows me random numbers between 1 and 10 (as it should) in the correct field, but the are not unique. (Ex/ Both row 1 and 5 could have the same value, say 6) I'm hopefully doing something simple wrong, so someone please point it out to me |