Jump to content

tjodolv

Members
  • Posts

    29
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

tjodolv's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Hi. I am trying to create a web-application with a member database. In the information for each member, is also information about what courses the member has. This is organized with one table for members, one for all existing courses, and a relations table containing the information on which course each member has. The courses are sorted by categories. Retrieving the data and listing it in a table is no problem, but when I want to edit a member I run into problems. The member information is loaded into a form similar to the "Create new member"-form I have set up. My problem starts when I try to fill inn what courses have been taken and when. Each course has a checkbox with an associated text field for when the member completed the course. The first – and only the first – category where the member has more than one course, will not pre-check the first course in that category. I believe it is related to my crummy, nested loops and arrays, but I have no idea how to make it work. Any pointers or better solutions are appreciated Categories (dbSelect() is a function with the following arguments ($select, $table, $where, $order_by, $limit) <?php $result = dbSelect("DISTINCT category","courses"," 1 = 1","weight"); $cCategories = array(); while ($row = mysql_fetch_assoc($result)) { $cCategories[] = $row['category']; } mysql_free_result($result); ?> This member's completed courses <?php $sql = "SELECT m.id, c.course_name, c.category, r.year FROM $members AS m LEFT JOIN $relations AS r ON r.member_id = m.id LEFT JOIN $courses c ON r.course_id = c.id WHERE m.id = $id ORDER BY c.category, r.year DESC"; ?> Set up some arrays to use in loops later on <?php $result = mysql_query($sql, $db['connection']); $courses = array(); $coursesCourseByCat = array(); $coursesCourseByYear = array(); $coursesYear = array(); while ($row = mysql_fetch_assoc($result)) { $courses[$row['category']][$row['course_name']] = $row['year']; $coursesCourseByCat[$row['category']] = $row['course_name']; $coursesCourseByYear[$row['year']] = $row['course_name']; $coursesYear[$row['course_name']] = $row['year']; } mysql_free_result($result); ?> Then the form, sorted by categories: - For each category, all courses of that category is fetched - For each course, we check to see if the member has completed this course <?php foreach ($cCategories as $key => $value) { $result = dbSelect("*","courses","category = '$value'"); $list = ""; while ($row = mysql_fetch_assoc($result)) { if ((in_array($row['course_name'],$coursesCourseByCat))||(in_array($row['course_name'],$coursesCourseByYear))) { // This is the conditional check I believe is related to the problem. It's awfully crummy... $y = $coursesYear[$row['course_name']]; $c = " checked=\"checked\""; } else { $y = ""; $c = ""; } $list .= "\n\t\t\t\t<tr><td><input type=\"checkbox\" name=\"".$row['course_name']."[1]\" value=\"".$row['id']."\" id=\"".$row['course_name']."_1\"$c />"; $list .= "<label for=\"".$row['course_name']."_1\">".$loc[$row['course_name']]."</label></td>"; $list .= "<td><input type=\"text\" name=\"".$row['course_name']."[2]\" id=\"".$row['course_name']."_2\" value=\"$y\" size=\"2\" /></td></tr>"; } $content .= "\t\t\t<table class=\"nmCourse\"><caption>".$loc[$value].":</caption>$list\n\t\t\t</table>\n"; } mysql_free_result($result); ?>
  2. I'm not really all that proficient with timezone issues, but there is a lot of info in the link I gave you. In short, you wwill have to know the timezone the timestamp was created with (probably the server's timezone, so that should be the same all the time) and the timezone of the person in question. Then you need to calculate the difference, apply it and display it.
  3. Including one file inside a .php file: <?php include "file.php"; // if file.php does not exist, this will print a notice, and continue the script include_once "file.php"; // if file has already been included, will not include again. Will also print a notice and continue if file.php does not exist require "file.php"; // if file.php does not exist, this will stop the script and print a Fatal Error. require_once "file.php"; // includes it once, and will print Fatal Error and stop the script if file does not exist ?> json I have no idea about...
  4. this should do it: <?php $readable_time = date("m-d-Y", $time); echo $readable_time; // reads, eg. 09-27-2007 ?> look here for more info on date/timestamp conversion: http://php.net/manual/en/function.date.php
  5. Ok. But why? 8M is quite a lot more than 1.7M ...?
  6. It's me, again... This problem may be a server problem, but I'll start by asking here. I have written a script for uploading images. When i attempt to upload images with a certain filesize, i get a fatal error. I tried with different filesizes, and narrowed down the approximate borderline to between 30k and 40k. A 76K .jpeg file works fine. A 1.7M .jpeg gives the error: Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 2608 bytes) in /Users/test/Sites/cms/lib/functions.inc.php on line 1050 A 20K .png file works fine. A 40K .png file gives the error: Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 3686400 bytes) in /Users/test/Sites/cms/lib/functions.inc.php on line 1056 The line in question is part of a function i found on the web: <?php // this function originally found at http://www.findmotive.com - a wee bit modified function cropImage($nw, $nh, $source, $stype, $dest) { $size = getimagesize($source); $w = $size[0]; $h = $size[1]; switch($stype) { case 'image/gif': $simg = imagecreatefromgif($source); break; case 'image/jpeg': $simg = imagecreatefromjpeg($source); // this is line 1050 break; case 'image/pjpeg': $simg = imagecreatefromjpeg($source); break; case 'image/png': $simg = imagecreatefrompng($source); // this is line 1056 break; } $dimg = imagecreatetruecolor($nw, $nh); $wm = $w/$nw; $hm = $h/$nh; $h_height = $nh/2; $w_height = $nw/2; if($wm> $hm) { $adjusted_width = $w / $hm; $half_width = $adjusted_width / 2; $int_width = $half_width - $w_height; imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h); } elseif(($wm <$hm) || ($wm == $hm)) { $adjusted_height = $h / $wm; $half_height = $adjusted_height / 2; $int_height = $half_height - $h_height; imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h); } else { imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h); } imagejpeg($dimg,$dest,100); return true; } // end cropImage ?> Settings read from phpinfo(): file_uploads On memory_limit 8M post_max_size 32M upload_max_filesize 32M SERVER_SOFTWARE Apache/2.0.59 (Unix) PHP/5.2.3 DAV/2 GD Support enabled GD Version bundled (2.0.34 compatible) What do I need to fix?
  7. Okay, so I decided to have another go at this... The problem seems to be that when $_POST is passed, for some reason the image array only gets passed with the first value. My code follows (exerpt). The form: <form action="" method="post" accept-charset="utf-8" enctype="multipart/form-data"> (...) <input type="hidden" name="approved" value="1" id="approved" /> <p><input type="file" name="image[]" accept="image/jpeg" /></p> <p><input type="text" name="imageTitle[]" maxlength="25" value="Title" /></p> <textarea name="imgDescription[]" rows="3" cols="40">Description...</textarea> <p><input type="button" name="more_images" value="Add another image" id="" onclick="moreImages()" /></p> (...) <p><input type="submit" name="new_page" value="Save" /></p> </form> I made the receiving script not do anything except a print_r($_POST); and print_r($_FILES); This gives me the following output (the whole form): <?php // $_POST Array ( [new_p_title] => Title [pri_cont] => Some content... [sec_cont] => 0 [tert_cont] => 0 [glist] => on [navigation] => on [img] => on [approved] => 1 [imageTitle] => Array ( [0] => Title ) [imgDescription] => Array ( [0] => Description... ) [new_page] => Save ) // $_FILES Array ( [image] => Array ( [name] => Array ( [0] => image_2.png ) [type] => Array ( [0] => image/png ) [tmp_name] => Array ( [0] => /Applications/MAMP/tmp/php/phpuLMpOu ) [error] => Array ( [0] => 0 ) [size] => Array ( [0] => 352906 ) ) ) ?> As you can see, only the firs value gets passed, even though i have added more images in the array (the form is simply shortened). However, in the "old" form and script gives me "correct" arrays when i print_r($_POST); and print_r($_FILES); (the above is basically just a translation from the old, which has Norwegian variable names and all that stuff...) Output from old script (exerpt): <?php // $_POST [bildeTittel] => Array ( [0] => Tittel [1] => Tittel ) [beskrivelse] => Array ( [0] => Beskrivelse... [1] => Beskrivelse... ) ) //$_FILES Array ( [bilde] => Array ( [name] => Array ( [0] => Bilde 2.png [1] => Bilde 3.png ) [type] => Array ( [0] => image/png [1] => image/png ) [tmp_name] => Array ( [0] => /Applications/MAMP/tmp/php/phpJRzevK [1] => /Applications/MAMP/tmp/php/phpIQOhTz ) [error] => Array ( [0] => 0 [1] => 0 ) [size] => Array ( [0] => 352906 [1] => 208167 ) ) ) ?> Any thoughts?
  8. Yep, as you can see in my second code example, the file input is named name="bilde[]" (or name="image[]" in the form for my first example). I am sure the solution is quite simple, but I just can't seem to find the faulty line(s)...
  9. I am not really sure what you mean... The thing is, i did this once before, this is basically just a translation of my previous script (had all the variables and stuff in Norwegian..) So it's a translated copy of a previous script, and the previous one works! I'll post some more code: Old stuff (form and script) - Norwegian... <p><input type="file" name="bilde[]" accept="image/jpeg" /></p> <p><input type="text" name="bildeTittel[]" maxlength="25" value="Tittel" /></p> <p style="margin-bottom: 15px;"><textarea name="beskrivelse[]" rows="2" cols="40">Beskrivelse...</textarea></p> <div id="myDiv"> </div> <input type="hidden" value="0" id="theValue" /> <p><a href="javascript:;" onclick="flereBilder();" title="Enda et bilde...">Nytt bilde...</a></p> <?php $feil = ""; $sidenavn = mysql_escape_string($_POST['sidenavn']); // vi har et array med filer. leser dem inn via en foreach-løkke foreach ($_FILES["bilde"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["bilde"]["tmp_name"][$key]; $tmp = strtolower($_FILES["bilde"]["name"][$key]); $ulovlig = "[\!|\?|\*|\||\&|\#|\^| |\,|\+|\$\(|\)|\[|\]|\{|\}]|%|/"; $tmp = ereg_replace($ulovlig, "_", $tmp); $norske = array("Æ", "Ø", "Å", "æ", "ø", "å"); $engelske = array("e", "o", "a", "e", "o", "a"); $name = str_replace($norske, $engelske, $tmp); if (($_FILES["bilde"]["type"][$key] != "image/jpeg") && ($_FILES["bilde"]["type"][$key] != "image/pjpeg") && ($_FILES["bilde"]["type"][$key] != "image/gif") && ($_FILES["bilde"]["type"][$key] != "image/png")) { $feil .= $name . " "; continue; } $bildeinfo = getimagesize($tmp_name); // må ha litt info om bildets dimensjoner til databasen if (($bildeinfo[0] > 800) || ($bildeinfo[1] > 800)) { $tmq = $bildeinfo[0]; if ($bildeinfo[0] < $bildeinfo[1]) { $tmq = $bildeinfo[1]; } $tmp = $tmq / 800; $cropbredde = $bildeinfo[0] / $tmp; $crophoyde = $bildeinfo[1] / $tmp; cropImage($cropbredde, $crophoyde, $tmp_name, $_FILES["bilde"]["type"][$key], "../$sidenavn/$name"); } else { move_uploaded_file($tmp_name, "../$sidenavn/$name"); } // så må vi lage et lite bilde til forhåndsvisningen $cropbredde = 150; // maks minibilde dimensjoner $crophoyde = 150; if ($bildeinfo[0] < $bildeinfo[1]) { $cropbredde = 100; $crophoyde = 150; } else { $cropbredde = 150; $crophoyde = 100; } $minibilde = "../$sidenavn/lite_$name"; // så lager vi det croppede bildet cropImage($cropbredde, $crophoyde, "../$sidenavn/$name", $_FILES["bilde"]["type"][$key], $minibilde); // filen er lastet opp og thumbnail er lagd, må legge inn info i databasen // fjerner skumle saker $tmp = htmlspecialchars($_POST['bildeTittel'][$key]); $bildeTittel = mysql_escape_string($tmp); $tmp = htmlspecialchars($_POST['beskrivelse'][$key]); $bildeBeskrivelse = mysql_escape_string($tmp); $tmp = htmlspecialchars($_POST['godkjent']); $bildeGodkjent = mysql_escape_string($tmp); $sql = "INSERT INTO " . $tabellPrefiks . "_bilder (adresse, galleri, tittel, beskrivelse, bredde, hoyde, godkjent) VALUES ( " . "'" . $name . "', " . "'" . $sidenavn . "', " . "'" . $bildeTittel . "', " . "'" . $bildeBeskrivelse . "', " . "'" . $bildeinfo[0] . "', " . "'" . $bildeinfo[1] . "', " . "$bildeGodkjent" . ")"; mysql_query($sql, $tilkobling); } // slutt if } // slutt foreach ?> This script works. The javascript stuff in the form is for adding more images to the form. I may just be too tired to see some translation error, but the funny thing is that that would probably break the whole script, and the first image always gets uploaded... Please help a poor n00b Off to bed, will look at it again tomorrow...
  10. Hi. I'm trying to make a script upload several images at once. I have a form which creates an array of files, with title and description for each file. The following script is supposed to handle the files properly, one by one, but it stops after the first image. I've tried with different images, different filetypes and so on, and I am basically stuck. What am I missing? <?php $imageError = ""; foreach ($_FILES["image"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["image"]["tmp_name"][$key]; $tmp = strtolower($_FILES["image"]["name"][$key]); $illegalChars = "[\!|\?|\*|\||\&|\#|\^| |\,|\+|\$\(|\)|\[|\]|\{|\}]|%|/"; $tmp = ereg_replace($illegalChars, "_", $tmp); $langSpecificChars = array("Æ", "Ø", "Å", "æ", "ø", "å"); $english = array("e", "o", "a", "e", "o", "a"); $name = str_replace($langSpecificChars, $english, $tmp); if (($_FILES["image"]["type"][$key] != "image/jpeg") && ($_FILES["image"]["type"][$key] != "image/pjpeg") && ($_FILES["image"]["type"][$key] != "image/gif") && ($_FILES["image"]["type"][$key] != "image/png")) { // this is for weeding out non-images and then show which files that were not accepted $imageError .= $name . " "; continue; } $imgInfo = getimagesize($tmp_name); // images larger than 800x600 should be resized to save bandwidth/diskspace, and be nice to people with small monitors if (($imgInfo[0] > 800) || ($imgInfo[1] > 800)) { $tmq = $imgInfo[0]; if ($imgInfo[0] < $imgInfo[1]) { $tmq = $imgInfo[1]; } $tmp = $tmq / 800; $cropwidth = $imgInfo[0] / $tmp; $cropheight = $imgInfo[1] / $tmp; cropImage($cropwidth, $cropheight, $tmp_name, $_FILES["image"]["type"][$key], "../$page/$name"); } else { move_uploaded_file($tmp_name, "../$page/$name"); } // image resized (if needed) - moving on to the thumbnail $cropwidth = 150; // max thumbnail dimensions $cropheight = 150; if ($imgInfo[0] < $imgInfo[1]) { $cropwidth = 100; $cropheight = 150; } else { $cropwidth = 150; $cropheight = 100; } $thumbnail = "../$page/thumb_$name"; cropImage($cropwidth, $cropheight, "../$page/$name", $_FILES["image"]["type"][$key], $thumbnail); // image and thumbnail done, insert info into db $tmp = htmlspecialchars($_POST['imageTitle'][$key]); $imageTitle = mysql_escape_string($tmp); $tmp = htmlspecialchars($_POST['imgDescription'][$key]); $imageDescription = mysql_escape_string($tmp); $tmp = htmlspecialchars($_POST['approved']); $imageApproved = mysql_escape_string($tmp); $sql = "INSERT INTO ".$dbTablePrefix."_images (location, gallery, title, description, width, height, approved) VALUES ( " ."'".$name."', " ."'".$page."', " ."'".$imageTitle."', " ."'".$imageDescription."', " ."'".$imgInfo[0]."', " ."'".$imgInfo[1]."', " ."1" .")"; mysql_query($sql, $dbConnect); } // end if } // end foreach ?>
  11. Hello. I am trying to make a form with optional elements. To cut down on screen-space-usage, I figured the best thing to do would be to hide the elements onload, and then allow the user to show the ones he/she wants to use. I use scriptaculous/prototype to do the showing and hiding, and that works fine. My problem is creating a function that toggles show/hide when the user checks/unchecks a checkbox. eg. if the user checks the box, show the extra content, but if the user then unchecks, hide it again... How do I go about getting that to work? I am at a dead end..
  12. Ah, ok. Didn't know that about MySQL. Thanks for the help
  13. The file approach could work, I guess. I knew how to get the specific thing I wanted from the DB, maybe I was a bit unclear in my question. I would like to get all the values at once, a "SELECT * FROM table" and put it in an array I can reuse, to minimize the amount of queries executed every time the page is loaded.
  14. edit: I see I was too slow and not so elegant, but if you want to understand what's happening "behind" the ternary operators used in the above examples, take a look at mine. It's basically the same, only longer... If you echo the table using a loop, all you need to do is add a counter and a check for whether the current number is odd or even, and apply a css class to the current row: <?php $sql = "SELECT * FROM table"; $result = mysql_query($sql, $connection); $i = 1; // this is the counter - make sure you set it _outside_ the loop! while ( $row = mysql_fetch_assoc($result) ) { if ($i % 2 = 0) { // we check to see if the counter is en even number. if so - blue background $style = "blue"; } else { // if it's not, the background becomes red $style = "red"; } echo "<tr style=\"background: $class\"><td>$row[]</td></tr>"; // print the row $i++; // make sure this addition happens _inside_ the loop! } ?>
  15. Hello, and thanks for reading I am practicing my php skills, and i decided to write a simple CMS. I was planning to use a table in the database to store all the settings, but how do I go about retrieving them in a single query to an array? For example, i have a table like so: ---------------------------------------- propertyvalue allow_guest_commentsyes maintenance_modeno file_uploadsno ---------------------------------------- And so on and so forth. My question is: how do I retrieve this information to an array that makes it easy to use it further on? For example: <?php if ($settings['allow_guest_comments'] == 1) { // show the comment form } ?> Or something along those lines..
×
×
  • 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.