Jump to content

halosinfire

Members
  • Posts

    15
  • Joined

  • Last visited

Everything posted by halosinfire

  1. I've never quite understood the syntax of preg_replace and now I need to convert the following line of code from ereg_replace to preg_replace because ereg_replace is deprecated. Can anyone help? $qstr = ereg_replace("&page=[0-9]+","",$_SERVER['QUERY_STRING']); Any help would be greatly appreciated.
  2. I'm sorry, the images don't load up for me.
  3. For some reason it won't let me edit my above post. Here's what I tried to write afterwards: I've done shopping carts before when each item didn't have any special attributes like color or size, but my current project has a color attribute. In the past I used Session variables named after the product ID to hold the quantity. Like so: /* initialize all quantities to 0 */ for ($i=0; $i <=$maxID; $i++) { if (!isset($_SESSION['product'.$i])) $_SESSION['product'.$i] = 0; } /* keeps a running total of the number of products to be purchased */ for ($j=0; $j<=$maxID; $j++) { $cartCount += $_SESSION['product'.$j]; } and on the products page I have: if (isset($_POST['addtocart'])) { $_SESSION['product'.$id] += 1; header ("Location: products.php?id=$id"); } Then on the checkout page I used a for loop to go through the cart by $id. However now I'm stuck because not only do I need to save the quantity of each product, but also the color, with the item # being the same. Can anyone help me transform the above code into something that will do the same thing, with the added feature of also keeping track of the color ordered? For example, I might purchase one green item #1, and one blue item #1. These have to be shown separately as the color is different, even though the item number is the same. Any help would be greatly appreciated. Sorry again if I didn't explain my problem well, going on 17 hours now!
  4. I have an online store site with an "Add to Cart" button on each products page. There is no dropbox or input box to select the quantity, so if you wanted two of the item, you would have to click the Add to Cart button twice. Confusing this a little more is that each item has a color option, however, the item ID is the same, and each item has different color options. We'll call the products wingbats for example's sake. From what I've read online, I need to create a session array to store the quantity and the color of the item purchased. I don't have any code to post because none of what I have tried has worked, so it's pretty useless. Here's some pseudo-code though with an example: The user is browsing product #1, color green. So i have those saved in variables from query strings: $id = 1; $color = green; The user presses the Add to Cart button once, so now one green wingbat should be added to the cart. I've tried something like: $_SESSION[$id][$color] += 1; but that didn't work. Then the user wants to buy a blue wingbat, same item id #1. It should add $_SESSION[$id][$color] +=1; with $color now being blue. Can anyone help me? Sorry if this post doesn't make a lot of sense, I'm a beginner when it comes to PHP and I've been going at this problem now for about 16 hours now with no sleep. Any help would be greatly appreciated.
  5. I just watched all of them...He's built a file upload script, which I already have, for uploading a single file. It doesn't really help solve my problem. However, thanks for the laughs - his random s*** and motherf***** cracked me up.
  6. Hi everyone - thanks for taking the time to read this. I have a form with 35 file input boxes as part of a CMS for my customer to upload 35 pictures of each his products. The breakdown of that is 7 pictures of the black version, 7 pictures of the blue, 7 pictures of the grey, 7 pictures of the red, and 7 pictures of the white version of each product. So that's 35 total pictures he needs to upload. Additionally, for each of those files that he uploads, a smaller "thumbnail" sized picture needs to be made. I have a file upload script that I always use that works beautifully - when there's one file to upload. I'm not sure how to apply it in this case for 35 files. Each input box has a unique name (black1, black2...black7, blue1, blue2...blue7, etc) so, technically I could repeat the upload code 35 times with the unique name of each file input box to do this, but that is obvoiusly extremely inefficient. I'm hoping someone here can help me out with a better solution. An additional requirement is that the names of the files be stored in a database. All of the filenames of the black pictures should be put into a string, separated by commas, and stored in the `blackpics` column of the database. All of the filenames of the blue pictures should be put into a string, separated by commas, and stored in the `bluepics` columns of the database. And so on for the grey, red, and white pictures. Here is the code that I have now to upload one file. It gets the file from input box "file", checks that it's of the right extension (an image file), checks the filesize, creates a random file name with a random number and timestamp, creates a thumbnail (448px x 298px - big thumbnail, I know), checks that the original image uploaded was of the right dimensions (873px x 581px), and if everything is okay, I end up with the big file saved in ../images/store/big/ and the thumb saved in ../images/store/small/. They both have the same filename, they're just stored in different directories. Temporary files are deleted and all that, and if there are any errors, the files are deleted. As I said, this works great for one file. So what I need to do is modify the code so that it does all of this for input box "black1", "black2"..."black7", then saves all the filenames into a string (black1.jpg,black2.jpg,black3.jpg,black4.jpg,black5.jpg,black6.jpg,black7.jpg) which I can then store in the 'blackpics' column of the database. Same for the blue, grey, red, and white. I don't need any help with the database part. I'm thinking that I need to create a function containing the file upload script that returns the filename. Then call that function 35 times, one for each of the input boxes. But I could be wrong. If anyone could offer me any assistance, I would greatly appreciate it. Here is the code for the upload script: <?php $filename = $_FILES["file"]["name"]; $file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention $file_ext = substr($filename, strripos($filename, '.')); // get file name $filesize = $_FILES["file"]["size"]; $allowed_file_types = array('.jpg','.gif','.png', '.JPG'); if (in_array($file_ext,$allowed_file_types) && ($filesize < 1024000)) { // rename file $rand = rand(1,100000000); $time = time(); $newfilename = $rand . $time . $file_ext; if (file_exists("../images/store/big/" . $newfilename)) { // file already exists error $err[] = "You have already uploaded this file."; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "../images/store/big/" . $newfilename); $pathToImage = '../images/store/big/' . $newfilename; $pathToThumb = '../images/store/small/' . $newfilename; $last4 = substr($pathToImage, -4); switch(strtolower($last4)) { case '.jpeg': $img = imagecreatefromjpeg($pathToImage); break; case '.jpg': $img = imagecreatefromjpeg($pathToImage); break; case '.png': $img = imagecreatefrompng($pathToImage); break; case '.gif': $img = imagecreatefromgif($pathToImage); break; default: exit('Unsupported type: '. $pathToImage); } $max_width = 448; $max_height = 298; // Get current dimensions $old_width = imagesx($img); $old_height = imagesy($img); // Calculate the scaling we need to do to fit the image inside our frame $scale = min($max_width/$old_width, $max_height/$old_height); // Get the new dimensions $new_width = ceil($scale*$old_width); $new_height = ceil($scale*$old_height); $tmp_img = imagecreatetruecolor($new_width, $new_height); imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height); switch(strtolower($last4)) { case '.jpeg': imagejpeg($tmp_img, $pathToThumb); break; case '.jpg': imagejpeg($tmp_img, $pathToThumb); break; case '.png': imagepng($tmp_img, $pathToThumb); break; case '.gif': imagegif($tmp_img, $pathToThumb); break; default: exit('Unsupported type: '. $pathToImage); } imagedestroy($tmp_img); imagedestroy($img); } } elseif (empty($file_basename)) { $err[] = "Select a file to upload"; } elseif ($filesize > 1024000) { $err[] = "File size limit exceeded"; } else { $err[] = "File type not allowed"; unlink($_FILES["file"]["tmp_name"]); } list($width, $height) = getimagesize("../images/store/big/$newfilename"); if ($width != "873" || $height != "581") { $err[] = "File dimensions error"; unlink("../images/store/big/$newfilename"); unlink("../images/store/small/$newfilename"); } ?> And in the body I have the file upload fields as so... <input type="file" name="black1" disabled="1"> <input type="file" name="black2" disabled="1"> ... <input type="file" name="black7" disabled="1"> <input type="file" name="blue1" disabled="1"> <input type="file" name="blue2" disabled="1"> ... <input type="file" name="blue7" disabled="1"> and so on for grey, red, and white. Like I said, if anyone can help me out, I would greatly appreciate it. And if you made it all the way down here, thanks again for taking the time to read all of this.
  7. It works! Barand you are truly a guru! I can't tell you how much I appreciate your time and your help! I have some other stuf to do with it now but I'm sure I can manage on my own. Thank you again for you help!
  8. Barand - You are awesome. I just woke up (i'm on Asian time). I'll give this a try now and see how it goes!
  9. Okay, the first step is working. Thank you barand. I changed your code to count the right and wrong answers instead of echoing "correct" and "wrong". Here's what I got: if(isset($_POST['checkQuiz'])) { $correctAnswers = 0; $wrongAnswers = 0; $idList = join (',', array_map('intval', array_keys($_POST['answer']))); $sql = "SELECT id, correctAnswer FROM quizzes WHERE id IN ($idList) "; $res = mysql_query($sql) ; while (list($id, $correct) = mysql_fetch_row($res)) { if ($correct == $_POST['answer'][$id]) { $correctAnswers +=1; } else { $wrongAnswers +=1; } } } Then in the body I display the score like this: <p> <?php $numberOfQs = $correctAnswers + $wrongAnswers; $score = round(($correctAnswers / $numberOfQs) * 100); ?> Correct Answers: <?php echo $correctAnswers; ?> <br> Wrong Answers: <?php echo $wrongAnswers; ?> <br> Score: <?php echo $score; ?> </p> Now the next step, which seems quite difficult to me, is to redisplay the quiz, this time without the radio buttons. I want to display a check mark image next to the questions that were answered correctly, and an X image next to the questions that were incorrect. I know I would need to do something like have a PHP variable holding the URL for the images. I.E $img = "correct.gif"; for a correct answer and $img = "incorrect.gif"; for incorrect answers. Then when I redisplay the quiz, I would do something like this: <?php $questionNumber = 1; $query = mysql_query("SELECT * FROM quizzes WHERE quizName = '$quizName'"); while ($result = mysql_fetch_array($query)) { ?> <p> <?php echo $questionNumber . ") " . $result['question'] . "<img src = '" . $img . "'><br>"; ?> <?php echo $result['answerA']; ?> <br> <?php echo $result['answerB']; ?> <br> <?php echo $result['answerC']; ?> <br> <?php echo $result['answerD']; ?> <br> </p> <?php $questionNumber +=1; } ?> How can I pass the $img variable down to each of the corresponding questions when redisplaying the quiz?
  10. Hey Barand - Wow that definitely worked. I'm going to edit it a little and post up what I got. I'm still only about halfway to what I want the final result to look like! I'll post again in a couple of minutes! Thank you so much!
  11. Well, i noticed one thing, I forgot to put mysql_query in this line of the logic: $query = mysql_query("select correctAnswer from quizzes where id = '$answerID'"); However, it still doesn't work. Gives me a 100 every time!
  12. Hi Barand, I have changed that portion of the code as shown in the second post. How do I pull them out of that array one by one and check them against the correct answer in the database? By the way, the IDs aren't necessarily in order like 1, 2, 3, as the CMS allows questions to be added and deleted to each quiz arbitrarily. For example, the ID of question number one could be 6, the ID for question number 2 might be 10, and so on. I'm not sure if that makes a difference. Again, many thanks for your help.
  13. Hey computermax, I would like to display all of the questions on the same page. The way I have the loop set up right now, they all display on the same page. What I can't get to work is the logic when the submit button is pushed. Any way you can help me would be greatly appreciated! Barand - Yes, that's what the ID is for. I just don't know how to loop each question once the submit button is pushed. For the logic, I've tried lots of different things. Right now what I have, which doesn't work, is the code below. I'm starting off small, just counting how many correct and incorrect answers there are. Later on I will expand that to show the questions again and display either a check mark if the answer was right, or an X if the answer was wrong. But that's getting ahead of myself. Right now I just want to be able to count the number of correct and incorrect answers. Small steps first! if(isset($_POST['checkQuiz'])) { $correctAnswers = 0; $wrongAnswers = 0; foreach ($_POST['answer'] as $answerID) { $query = ("select correctAnswer from quizzes where id = '$answerID'"); while ($result = mysql_fetch_array($query)) $ans = $result['correctAnswer']; // get the correct answer A, B, C, or D if ($_POST['answerID'] == $ans) $correctAnswers +=1; if ($_POST['answerID'] != $ans) { $wrongAnswers +=1; } } } And in the body: <form method="post" action="quizzes.php"> <?php $questionNumber = 1; $query = mysql_query("SELECT * FROM quizzes WHERE quizName = '$quizName'"); while ($result = mysql_fetch_array($query)) { ?> <p> <?php echo $questionNumber . ") " . $result['question'] . "<br>"; ?> <input type="radio" name="answer[<?php echo $result['id'] ?>]" value="A"> <?php echo $result['answerA']; ?> <br> <input type="radio" name="answer[<?php echo $result['id'] ?>]" value="B"> <?php echo $result['answerB']; ?> <br> <input type="radio" name="answer[<?php echo $result['id'] ?>]" value="C"> <?php echo $result['answerC']; ?> <br> <input type="radio" name="answer[<?php echo $result['id'] ?>]" value="D"> <?php echo $result['answerD']; ?> <br> </p> <?php $questionNumber +=1; } ?> <input type="submit" name="checkQuiz" value="Check Quiz"> </form> The flawed logic above shows me getting all the questions right, even when I purposely choose the wrong answers! Again, any help anyone could give me would be greatly appreciated!
  14. Hi everyone, this is my first post here. I need some help with a PHP/MySQL multiple choice quiz that I'm making for a website. I have succesfully created a CMS to create new quizzes and then add questions and answers for it to a MySQL database table. I have written the code to loop through and display the questions and possible answers with radio buttons. I'm having trouble with the logic of correcting the quiz however. I've been searching online and trying different things for 2 days, but nothing I've come up across has helped me. I don't want to just download someone else's multiple choice quiz package, I want to be able to figure this out on my own (well, with your help!) because once I get the logic down, that's only half of what I need to do. No one else's pre-written package suits my needs. Well here goes. I have a CMS that creates a quiz and then adds questions and answers. The database table has the following fields: [id] [quizName] [question] [answerA] [answerB] [answerC] [answerD] [correctAnswer] On my quiz page, I have a dropdown box to select which quiz you want to take. Then I query the database looking for questions which have that quiz name. Here's my code: <form method="post" action="quizzes.php"> <?php $questionNumber = 1; $query = mysql_query("SELECT * FROM quizzes WHERE quizName = '$quizName'"); while ($result = mysql_fetch_array($query)) { ?> <p> <?php echo $questionNumber . ") " . $result['question'] . "<br>"; ?> <input type="radio" name="answer<?php echo $result['id'] ?>" value="A"> <?php echo $result['answerA']; ?> <br> <input type="radio" name="answer<?php echo $result['id'] ?>" value="B"> <?php echo $result['answerB']; ?> <br> <input type="radio" name="answer<?php echo $result['id'] ?>" value="C"> <?php echo $result['answerC']; ?> <br> <input type="radio" name="answer<?php echo $result['id'] ?>" value="D"> <?php echo $result['answerD']; ?> <br> </p> <?php $questionNumber +=1; } ?> <input type="submit" name="checkQuiz" value="Check Quiz"> </form> This displays the questions and answers nicely. I know I need some kind of unique identifier for each question as I'm displaying them all from a single while loop. Then when the submit button is clicked, I need to get the answer selected for each of the questions, and compare it to the correctAnswer field in the database for that question. Nothing I've tried has worked. Can anyone help me out. Even if you give me some pseudo code, I'll code it up myself and see if it works. If it doesn't, I'll paste it here to show you what I've done. Any help would be greatly appreciated! I spent about 20 hours on this to no avail and nothing I've found searching Google has helped me either.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.