HaikelChong Posted June 22, 2011 Share Posted June 22, 2011 Code:<?php $resultName = mysql_query("SELECT name FROM tblUser ORDER BY totalCount DESC"); while($name = mysql_fetch_array($resultName)){ echo $name['name']; } ?>Hi. As you can see, I'm trying to get 'name' by order of totalCount DESC into variable $resultName. What my codes does is that it echoes out all the names. What i want to do is to echo the first value of name which has the highest totalCount. Then echo second value which has the second highest totalCount. and so on. Any help would be greatly appreciated. Thanks in advance. Link to comment Share on other sites More sharing options...
MrFish Posted June 22, 2011 Share Posted June 22, 2011 mysql_fetch_array gets an array where the index is numeric. i.e. 1, 2, 3, 4, etc.You want to use mysql_fetch_assoc where the index is the key of the variable. i.e. "name", "id", etc.To get the highest value use LIMIT 0, 1 after DESC Link to comment Share on other sites More sharing options...
HaikelChong Posted June 22, 2011 Author Share Posted June 22, 2011 Thank you! It works perfectly! Link to comment Share on other sites More sharing options...
thescientist Posted June 22, 2011 Share Posted June 22, 2011 it might be worth pointing out that in computer languages, indexes start at 0 Link to comment Share on other sites More sharing options...
HaikelChong Posted June 23, 2011 Author Share Posted June 23, 2011 Thank you, the scientist. Don't mind me asking another. I'm trying to get a hyperlink to call a php function. Is this the way to do it? I tested and it printed out 'no run'.<?phpfunction updateCount1(){mysql_query("UPDATE tblUser SET totalCount = totalCount + 1" . "WHERE userID = '" . $captureID1 . "'");echo $captureID1;} //end of updateCount1//random first person$randUser = rand(1,6);$resultUser = mysql_query("SELECT * FROM tblUser WHERE userID = '" . $randUser . "'");while ($row = mysql_fetch_assoc($resultUser)){$userID = $row['userID'];//$image = $row['image'];$name = $row['name'];}$captureID1 = $userID;if (isset($_GET['run'])) $linkchoice=$_GET['run'];else $linkchoice='';switch ($linkchoice){case 'first':updateCount1();break;case 'second':updateCount2();break;default :echo 'no run';}?><a href="?run=updateCount1"> Link to comment Share on other sites More sharing options...
Ingolme Posted June 23, 2011 Share Posted June 23, 2011 You probably meant to make your link like this:<a href="?run=first">or this:<a href="?run=second"> Link to comment Share on other sites More sharing options...
HaikelChong Posted June 24, 2011 Author Share Posted June 24, 2011 Yes thank you for that. But it still doesn't work. I guess the way i call the function works. But not the function itself. In the following codes, I'm able to print out $name. But not $userID. Is there a reason why? FYI userID is the primary key in tblUser.<?php //random first person $randUser = rand(1,6); $resultUser = mysql_query("SELECT * FROM tblUser WHERE userID = '" . $randUser . "'"); while ($row = mysql_fetch_assoc($resultUser)) { $userID = $row['userID']; //$image = $row['image']; $name = $row['name']; } //$captureID1 = $userID;?> Link to comment Share on other sites More sharing options...
Ingolme Posted June 24, 2011 Share Posted June 24, 2011 There's no reason why it shouldn't print. Where's the line where the value is actually being printed? Link to comment Share on other sites More sharing options...
thescientist Posted June 24, 2011 Share Posted June 24, 2011 what about the code to connect to the database? where's that, and are you checking it for errors? Link to comment Share on other sites More sharing options...
HaikelChong Posted June 27, 2011 Author Share Posted June 27, 2011 Ingolme, I'm not really sure what you meant. I tried printing $userID the same way i did for $name. It works for $name but not for $userID. $userID shows nothing.thescientist, the code to connect to the database? I managed to print $name so there shouldn't be any problem with the connection to the database right? Correct me if i'm wrong.This is how i tried printing the variable. <div class="profilepic-right"><?php echo $name; ?></div>Yes it does display $name. But if i replaced it with $userID, it displays nothing. Thank you for any further help. Link to comment Share on other sites More sharing options...
Ingolme Posted June 27, 2011 Share Posted June 27, 2011 Have you tried printing $row['userID'] to see if it's getting a value?You also might be changing the $userID value somewhere without meaning to. You haven't provided all your code so I can't know. Link to comment Share on other sites More sharing options...
HaikelChong Posted June 28, 2011 Author Share Posted June 28, 2011 Thank you ingolme! Although i still couldn't find the problem. I decided to do this instead since it prints out the same thing.$captureID1 = $randUser Link to comment Share on other sites More sharing options...
HaikelChong Posted June 28, 2011 Author Share Posted June 28, 2011 I'm so sorry for disturbing. But this really means a lot to me. The value in $randUser is the same as $captureID1. I CAN echo $captureID1 and $randUser. But when i put either variables into the update mysql query, it didn't update it. It does update though when i put any number. Any idea why? Thank you!<?php //random first person $randUser = rand(1,6); $resultUser = mysql_query("SELECT * FROM tblUser WHERE userID = '" . $randUser . "'"); while ($row = mysql_fetch_assoc($resultUser)) { $name = $row['name']; } $captureID1 = $randUser; function updateCount1() { mysql_query("UPDATE tblUser SET totalCount = totalCount + 1 WHERE userID = '" . $captureID1 . "'"); } //end of updateCount1 ?> <div class="profilepic-left"><?php echo $captureID1; ?></div> Link to comment Share on other sites More sharing options...
Ingolme Posted June 28, 2011 Share Posted June 28, 2011 That's because you're not passing the variable to the function. Also, I don't see you calling updateCount() anywhere.Functions don't access globalr variables and it's usually better if they don't, that way you can reuse the function with different values. function updateCount1($id) { return mysql_query("UPDATE tblUser SET totalCount = totalCount + 1 WHERE userID = '" . $id . "'");}updateCount($captureID1); Link to comment Share on other sites More sharing options...
HaikelChong Posted June 30, 2011 Author Share Posted June 30, 2011 Thank you! Sorry. I didn't post the calling function cause i thought it wouldn't be of much help. Anyhow, thank you so much for your much appreciated help Link to comment Share on other sites More sharing options...
HaikelChong Posted July 4, 2011 Author Share Posted July 4, 2011 Hello again. Let me give you a rundown on how the codes are supposed to work. When you first enter the page, it will random out a question and 2 names. Based on the question, for example 'Who is fatter?', you have to click either of the 2 hyperlinks based on your answer. One hyperlink will pass in one userID of the name and sames goes for the 2nd hyperlink. That hyperlink will then call a php function to add 1 to the totalCount of that userID. Also, after clicking the hyperlink, it refreshes the page and another set of random question and 2 names will be displayed. And the process continues.With the help i've gotten here, i managed to get all the parts working. Except for one. Problem: After randoming the first set of a question and 2 names and clicking the hyperlink based on my answer, it updated the totalCount of the second set instead of the first set like it was supposed to. My assumption: What i think is that, when i click the hyperlink, it randoms the second set first before calling the php function thus updating the totalCount of the second set, instead of the other way round.Is there any solutions besides using ajax? Guess it's the way how i call the php function using the hyperlink. Thanks in advance. <body><div id="page-wrapper"> <div id="header"> <div class="header-content"> <?php function updateCount1($id) { mysql_query("UPDATE tblUser SET totalCount = totalCount + 1 WHERE userID = '" . $id . "'"); } //end of updateCount1 function updateCount2($id2) { mysql_query("UPDATE tblUser SET totalCount = totalCount + 1 WHERE userID = '" . $id2 . "'"); } //end of updateCount2 //random question $randQuestion = rand(1,6); mysql_connect("localhost","root",""); mysql_select_db("game"); $resultQuestion = mysql_query("SELECT * FROM tblQuestion WHERE questionIDQS = '" . $randQuestion . "'"); while ($row = mysql_fetch_assoc($resultQuestion)) { $question = $row['question']; }?> <h1><?php echo $question; ?></h1> </div> </div> <div id="content"> <div class="content-left"><a href="index.php?run=first"><img src="images/pick_left.png" width="167" height="74" alt="Pick me!" /></a></div> <div class="content-mid"> <?php //random first person $randUser = rand(1,6); $captureID1 = $randUser; $resultUser = mysql_query("SELECT * FROM tblUser WHERE userID = '" . $randUser . "'"); while ($row = mysql_fetch_assoc($resultUser)) { $name = $row['name']; } ?> <div class="profilepic-left"><?php echo $name; ?></div> <?php //random second person $randUser = rand(1,6); $captureID2 = $randUser; //check for duplicates while ($captureID1 == $captureID2) { $randUser = rand(1,6); $captureID2 = $randUser; } $resultUser = mysql_query("SELECT * FROM tblUser WHERE userID = '" . $randUser . "'"); while ($row = mysql_fetch_assoc($resultUser)) { $name = $row['name']; } ?> <div class="profilepic-right"><?php echo $name; ?></div> </div> <?php //runs update function if (isset($_GET['run'])){ $linkchoice=$_GET['run']; } else $linkchoice=''; switch ($linkchoice) { case 'first': updateCount1($captureID1); break; case 'second': updateCount2($captureID2); break; default : echo 'no run'; } ?> <div class="content-right"><a href="index.php?run=second"><img src="images/pick_right.png" width="166" height="74" alt="Pick me!" /></a></div> </div> <br class="clear" /></div></body> Link to comment Share on other sites More sharing options...
HaikelChong Posted July 6, 2011 Author Share Posted July 6, 2011 Hi. Anyone willing to help? Link to comment Share on other sites More sharing options...
justsomeguy Posted July 6, 2011 Share Posted July 6, 2011 You're not putting the ID in the link, you're putting "first" or "second". Just print the actual ID.Also, "random" is not a verb. Link to comment Share on other sites More sharing options...
isttoptan Posted August 16, 2011 Share Posted August 16, 2011 thank you, nice sharing.. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.