PHP - How To Apply A Condition To The Display Of An Array
I have the following code that queries the database for the results of a quiz.
The display reports on completed quizzes, showing, line by line, the quiz title, the date of the quiz, an image of a green tick if the quiz result was correct or an image of a red cross for an incorrect result and a fractional result shoratio of correct answers. A student can repeat a quiz numerous times, and therefore his results for a particulat quiz can display several rows with green ticks and red crosses for the same test done at different times with differing results. I want to recode this so that the red crosses for incorrect results will no longer display once a student achieves a green tick result. I've had several goes at this but my results are less than encouraging. Can anyone help? Do I tackle this through an altered query, or should I include conditions in the display of the array? Code: [Select] <?php $query1 = mysql_query("SELECT DISTINCT quizTitle, userId, passState, userScore, totalScore, DATE_FORMAT(userDate,'%b %e, %Y') AS userDate FROM quiz WHERE managerId = '$managerId' AND userId = '$userId' ORDER BY quizTitle, userDate ASC"); while ($row = mysql_fetch_array($query1)) { echo "{$row['quizTitle']} <br />\n"; echo "{$row['userDate']} <br />\n"; if ("{$row['passState']}" == 1) {echo "<img src=' ../../wood/wood_tool_images/tick2.png' /><br />\n";} if ("{$row['passState']}" == 0) {echo "<img src=' ../../wood/wood_tool_images/cross2.png' /><br />\n";} echo "{$row['userScore']}".'/'."{$row['totalScore']} ?> Similar TutorialsI'm not sure what am I doing wrong? I want to check if match1, match2 or match 3 has "W" and the continues count ($numOfWinsCount) to be maximum of 2. If $numOfWinsCount is more than 2 do not continue with Code: [Select] $matchresult . $matchWcounter = "Qualified for Quarter Finals!"; My Code: Code: [Select] $match1 = "W" $match2 = "W" $match3 = "L" $getawayr="Group A"; if($getawayr=="Group A"){ for ( $matchWcounter = 1; $matchWcounter <= 3; $matchWcounter ++) { $numOfWinsCount = 0; if($match . $matchWcounter=="W" and $numOfWinsCount <3){ $numOfWinsCount=$numOfWinsCount+1; $matchresult . $matchWcounter = "Qualified for Quarter Finals!"; } } } how to display a lengthy form...20 lines of HTML, only if a particular condition is met? should i use if (condition==true) { $form=<<<startForm /HEREDOC HTML for form startForm; echo $form; } else { no html } is this a good way? Is there a way to apply the function trim() on a whole array, instead of applying it to the individual input fields one by one? Here's the code: $blurb1_title = array ( $_POST['blurb1_title1'], $_POST['blurb1_title2'], $_POST['blurb1_title3'], $_POST['blurb1_title4'], $_POST['blurb1_title5'] ); $blurb2_title = array ( $_POST['blurb2_title1'], $_POST['blurb2_title2'], $_POST['blurb2_title3'], $_POST['blurb2_title4'], $_POST['blurb2_title5'] ); $blurb3_title = array ( $_POST['blurb3_title1'], $_POST['blurb3_title2'], $_POST['blurb3_title3'], $_POST['blurb3_title4'], $_POST['blurb3_title5'] ); All those $_POST variables are input fields, and I would like to apply trim() to them so the spaces get removed. Is there an efficient way to do it without having to apply it all individually, if yes, what would be the correct syntax? [php] I'm a bit of a newb to PHP and MySQL. I seem to be having an issue with something. How do I loop through an array, querying each value in the array until the query meets a certain condition.. In this case it would be that the number of rows returned from the query is less than five. Here is what I have: $query1="SELECT UserID FROM Users where RefID='$userid'"; $result1=mysql_query($query1); while ($row = mysql_fetch_array($result1, MYSQL_NUM) && $sql2querynum < '5') { echo ($row[0]); echo " "; $sql2 = "SELECT * FROM Users WHERE RefID=$row[0]"; $sql2result = mysql_query($sql2); $sql2querynum = mysql_numrows($sql2result); } Problem is, for every value it echoes out, I get the following warning: mysql_numrows(): supplied argument is not a valid MySQL result resource Like I said, I'm a newbie to PHP to maybe I'm not even going about doing this the right way. Hoping someone can help to point me in the right direction. When I use the following code, it doesn't print out "test" until after I refresh the page. unset($_SESSION["product"]);
if ( !empty($_SESSION["product"]) && ($_SESSION["product"] != NULL)) { When I use: var_dump($_SESSION["product"]); It shows: array(0) { } I'm wondering it that's the problem. That it's array(0) when it should be array(). Hello all, I need to make a rotating banner system for a client. They want about 5 banners to rotate with Javascript. However they want the visitor to sort of step through each one on each refresh. So if I have 5 banners and I go to the site, I see banner 1 and then the javascript rotates through each one. When I go back to the site, they want it to start with banner 2 and then the javascript will rotate through each one. Then on and on. Go back and see banner 3, then step through again. I have a nice Javascript image slider I'd like to use. It basically just requires the images in order. Like: <img src="1.jpg"/> <img src="2.jpg"/> <img src="3.jpg"/> <img src="4.jpg"/> <img src="5.jpg"/> I've thought about it and I think the best way to do this would just be using PHP and a cookie. So when the viewer first goes to the site, PHP checks for the cookie. If it exists, it pulls the starting image #. If it doesn't, it starts at 0 and sets the cookie. As for the images part, I figured an associate array would work well. The key would be what the cookie logic is based on, and then the image HTML would be in the array. So you visit the site without a cookie it would be... 0 => <img src="1.jpg"/> 1 => <img src="2.jpg"/> 2 => <img src="3.jpg"/> 3 => <img src="4.jpg"/> 4 => <img src="5.jpg"/> Then when you go back to the site, I need the array to be re-ordered like this: 0 => <img src="2.jpg"/> 1 => <img src="3.jpg"/> 2 => <img src="4.jpg"/> 3 => <img src="5.jpg"/> 4 => <img src="1.jpg"/> What's the best way to go about this array logic? My experience with manipulating data inside arrays is rather limited. Basically I need it to say "Start with key # and then reorder array based on that" Hi, I'm hoping someone can help me with a simply array problem. I have a table with two columns: "ID" and "Name" and 100 Names. I also have an array called $myIDList containing 10 IDs. I want to display 10 Names from the table that correspond with the IDs from my array. Originally, I tried to implode the array, adding commas and inserted into my MySQL query using: $myQuery = "SELECT ID, Name FROM namesTable WHERE ID IN ($myImplodedIDList)"; The problem was that duplicated names (i.e. the same Name, but assiged to different IDs) would only be displayed once. So now I'm trying to use the original array to loop through a second array and display Names where the ID matches an ID from my first array. I have succesffuly listed the table like this: $myQuery = "SELECT ID, Name FROM myTable ORDER BY ID"; $myResult = mysql_query($myQuery) or die(mysql_error()); while($row=mysql_fetch_array($myResult)) { echo $row['ID']; echo $row['Name']; } But I want to do something like: while($row=mysql_fetch_array($myResult)) { while $row['ID'] = $myIDList { echo $row['Name'] } } Can someone shed some light onto this for me please? Thank you! Bryan Hi, Is it possible to display the whole array of voteID in this url? https://falling-skies-tv.loopiasecure.com/vote_result.php?voteID=292&comp=falling-skies-danmark Is there a script/website/code I can use? Thank you! my user pm table has a touserarray field which is a serialized array that looks like this: a:1:{s:2:"cc";a:1:{i:15773;s:14:"testusername";}} i know I can unserialize it with the unserialize() function. But I need to extract the value corresponding to 'i' field and the 's' field that has the username. So from the above array, i need to retrieve the value '15773' and 'testusername'. Can someone tell me how? $color=array("01 orange","02 blue","03 red"); If my array was structured this way where the first two characters represented a code, how would I be able to display the code and color distinctly? Hi, i'm getting an error when I load my php code in a browser. Here's my code snippet: <?php mysql_connect("localhost","root"); mysql_select_db("something"); $bellProductsArray=array(1=>"Apple_iPhone3GS.jpg",2=>"Apple_iPhone4.jpg"); $result=mysql_query("SELECT Name, Manufacturer FROM bellProducts WHERE ID=1"); $row=mysql_fetch_assoc($result); print "Name: {$row['Name']} Manufacturer: {$row['Manufacturer'] }"; ?> <img src=<?php array_values($bellProductsArray); ?> alt="Apple iPhone 3GS" title="Apple iPhone 3GS" /> **I can't get it to display the first image (eg: Apple_iPhone3GS.jpg), I made sure that I have the image in same directory. please help!1! I've been trying to get this to work for a few days now and can't seem to get anywhere with it! Any help is very much appreciated. I am creating a number generator where the user is required to insert the maximum number and generate rate. Maximum number is the highest number that the script will go to, and generate rate is how many numbers should be created. When the script runs, the numbers are stored into a MySQL database. The MySQL database creates a unique ID number so it can be recalled at a later date. The user then needs to go to Sorting.php and selects the record they wish to use. The numbers from this record are then counted and then stored into an array. If the numbers 2,5,5,0,2,2,1,3,5,1 where generated, the output of this new array will be: [5] => 3 [3] => 1 [2] => 3 [1] => 2 => 1 (These arrays can be of any length) I now need to extract this data and show it on screen. I don't know how to approach it. Format example: Number 5 appears 3 times Number 3 appears 1 times Number 2 appears 3 times Number 0 appears 1 times Sorting.php <?php $db1 = new Number_Information(); $db1->openDB(); $sql = "select * from GeneratedNumber"; $result = $db1->getResult($sql); if (!$_POST) { //page loads for the first time ?> <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> Select the ID (If you have just generated a number, it will be the last one): <!--This is the drop down menu--> <select name="ID"> <?php while ($row = mysql_fetch_assoc($result)) { echo "<option value='{$row['ID']}'> {$row['ID']} </option>"; } ?> </select> <br /> <input type="submit" value="Continue" /> </form> <?php } else { $ID = $_POST['ID']; //This will contain the value that was selected in the drop down menu above. $result = mysql_query("SELECT NUMBERS2 FROM GeneratedNumber WHERE ID='{$ID}'"); //This tells the script to select NUMBERS2 contents which is assosiated with the ID. while ($row = mysql_fetch_array($result)) $NUMBERS2 = $row['NUMBERS2']; echo "$NUMBERS2"; //Testing reasons - remove it later. echo "<br />"; //same as above echo "$ID"; // as above. echo "<br />"; //as above //Convert NUMBERS2 back into an array when inserted into table from Generator.php $NUMBERS = explode(",", $NUMBERS2); // Counts how many times a number appears //******************************************************************************************************************* //IF ALL ELSE FAILS, REVERT BACK TO THIS. //print_r(array_count_values($NUMBERS)); //It will only print the statement, but gives you a good building block when things WILL go wrong. //******************************************************************************************************************* rsort($NUMBERS); print_r(array_count_values($NUMBERS)); (The questions when editing posts are hard. I've needed to Google both of them:() Here is my current code to display files from the end to beginning : Code: [Select] <?php function func() { $date = date("Y-m-d"); $picarray = array(); $userarray = array(); $handle = opendir ('pqimages/'.$date.'/'); while (false !==($file = readdir($handle))) { if($file != "." && $file != "..") { $picarray[] = "http://blah.com/pqimages/".$date."/".$file.""; $userarray[]= "pqimages/".$date."/".$file.""; } } sort($picarray); sort($userarray); closedir($handle); $json_array = json_encode($picarray); echo $json_array; } func(); ?> How could I change this from end to beginning for ($picarray)? Cheers, George Hello I have an array that I would like to display as a family tree... and am looking for an example script that will allow me to do this. I want to be able to create X number of generations and fill the boxes with data from my array.... So, if I want 5 generations I want to generate a tree with 63 elements or boxes Any help would be appreciated Thanks Hi, It's my first time here. I'm not a coder but I sometimes need to find solutions by myself so here I am... I think this one will be easy for all of you. I generate a PDF displaying data from fields on a DB They all display very well except the multiple choices. Here is an exerpt of my code; I tried the implode code to display my values separated by commas but in the document it stil displays "Array" (screenshot enclosed) instead of something like: "Sante mentale, Reussite scolaire, Environnement sain " And I obviously don't know what's wrong...
<tr><td><?php echo $fields['prenomact']['value']; ?> <?php echo $fields['nomact']['value']; ?></td><td><?php echo $fields['titreact']['value']; ?></td></tr> Thanks in advance for your help, Valérie
Example: i have an array of names array{ => john [1] = ted [2] = jeff } and i display them like this: john, ted, jeff but jeff is currently logged in so i want his name to display first(i have my reasons lol) how would i go about this? i got as far as: if(in_array($user->user_name, $likes)) { $user_key = array_search($user->user_name, $likes); } then hit a brain freeze. Thanks I am trying to return all the photos in the database that has the albumid associated with that table info. I can echo the $album->id (albumid) no problem, but my query it think is somewhat off. Please anyone. Code: [Select] <div id="photo-items" class="photo-list-item"> <?php echo $album->id.'<br>'; // fetch album photos and ids $database =& JFactory::getDBO(); $query = "SELECT * FROM jos_photos where albumid = ".$album->id." ORDER BY ASC"; $photos = mysql_query($query); //This returns and array of photos, but then needs to display all the photos in loop below, but it not retuning none, even if there is 100 photos in table and in the folder directory if($photos) { for( $i=0; $i<count($photos); $i++ ){ $row =& $photos[$i]; ?> <div class="photo-item" id="photo-<?php echo $i;?>" title="<?php echo $this->escape($row->caption);?>"> <a href="<?php echo $row->link;?>"><img class="" src="<?php echo $row->getThumbURI();?>" alt="<?php echo $this->escape($row->caption);?>" id="photoid-<?php echo $row->id;?>" /></a> <?php if( $isOwner ) { ?> <div class="photo-action"> <a href="javascript:void(0);('<?php echo $row->id;?>');" class="remove"><?php echo JText::_('CC REMOVE');?></a> </div> <?php } ?> </div> <?php } } else { ?> <div class="empty-list"><?php echo JText::_('CC NO PHOTOS UPLOADED YET');?> <button class="button button-upload" href="javascript: void(0);&userid=88" id="upload-photos-button">Start Uploading</button></div> <?php } ?> </div> |