Jump to content

Bizty

Members
  • Posts

    16
  • Joined

  • Last visited

    Never

Everything posted by Bizty

  1. I've never really gotten the hang of the foreach() function (compared to while), but it seems that foreach() is used alot when you need to go through an array of sorts. I've now realised thats really to bad, and seeing I want to use the loop in a function I'd better get it right ^^ I have a function called CheckEmpty(), what it does is basicly checking if the supplied variable is empty. The issue is I have plenty variables (in the area of 100 for this particular script) that needs this check, so I wanted to do a simple check rather than: function CheckEmpty($var1, $var2, $var3 ..... $var99) { } so I was hoping I could do it dynamicly (maybe with an array?) where I simply go through all supplied vars. I guess it would look something like: function CheckEmpty() { foreach(something I dont get) { if(empty($var)) { return true; } else { return false; } } }
  2. Malevolence: didn't work encasing it in quotes. Why is it a better practice (thanks for the tip, just want to know the explanation ) Mark Baker: I'm sorry I don't follow there - how do I include the array then?
  3. Not the issue I'm afraid, as the one I posted here was a quick rewrite, I'll just give you the actual function as it is in my file, where it actually is closed =( function WriteError($error_id) { if(empty($error_id)) { print "Error: no error id was supplied"; die; } else { print "<p style='font: verdana;font-size:10px;font-weight:bold;color:red;'>Error: ". $error[$error_id] ."</p>"; } }
  4. Right now it's merely just being called if there's a $_GET value. //URL: somesite.php?e=1 if($_GET['e']) { WriteError($_GET['e']); } I've already checked if the error id gets into the script and if I write it like this: function WriteError($e_id) { if(empty($e_id)) { print "Error: empty"; } else { print "Error: ". $error[$e_id] ."; } print $e_id; // writes the number } As for now this only handles 1 error I'm aware of that, but first I need it to write anything at all ^.-
  5. I've setup a file called errors.php where I've set the errors up in an array: $error = array(); $error[0] = "Some error"; $error[1] = "Error 1"; now if I say print $error[0]; in that file it works fine. But the thing is I'm including the error.php into another file: <? functions.php: include('errors.php'); function WriteError($e_id) { if(empty($e_id)) { print "Error: empty"; } else { print "Error: ". $error[$e_id] ."; } } ?> then it simply only writes 'Error:' - Any ideas?
  6. Ok, now I'm quite puzzled by this one (I think I'm having mind tricks now a days). I have a simple stat function that only checks if the IP is in the DB if it is, then add a visit and if not, add it as a new entry, code: function GatherStats($ip) { $insert_q = mysql_query("SELECT * FROM stats"); if(mysql_num_rows($insert_q) == 0) { mysql_query("INSERT INTO stats (ip, visits) VALUES ('$ip','1')"); } else { while($irow = mysql_fetch_array($insert_q)) { $new_visits = 0; if($ip == $irow['ip']) { $new_visits = $irow['visits'] + 1; mysql_query("UPDATE stats SET visits='$new_visits' WHERE ip='$ip'"); } else { mysql_query("INSERT INTO stats (ip, visits) VALUES ('$ip', '1')"); } } } } And on the index: $ip = $_SERVER['REMOTE_ADDR']; GatherStats($ip); so that in my (maybe blind) eyes that looks okay, but what there's really happening is that: 1: It gets the IP 2: It checks in the DB if there's a duplicate 3: If there is, it adds mysql_num_rows(); - 1(the duplicate) new entries in the DB, _instead_ of just adding a new visit. so it seems that if it fills the requirements in the if($ip == $irow['ip']) then it goes to the }else{ I am obviously missing something but I am oblivous to what (too much coding lately I reckon)
  7. I'm not strong in the image scripts so in all honesty I didnt make all of this, so there may be duplicate code. I tried cleaning it my self and got a few errors. With regards to upping my memory, I would, but the host it's at has limited to 24mb :/
  8. You dont need to echo your include just: session_start(); if(!$_SESSION['callsign] && !$_SESSION['password'] && !$_SESSION['email'] && !$_SESSION['name']) { include('content/side.php'); } else { include('content/pilot.php'); }
  9. I have a script that uploads and rezises and image aswell as make a thumb of it. But there's an issue, the script is for some reason using over 24mb memory which causes an error (the max script memory allowed is fixed), I cant seem to find the error my self so hopefully you'd be able to. Code: <? function AddImages() { $i = 1; while($i <= 10) { $image_name = "image_" . $i; if (isset ($_FILES[$image_name])){ $imagename = $_FILES[$image_name]['name']; if($imagename == "") { $i++; } else { $source = $_FILES[$image_name]['tmp_name']; $target = "images/gallery/".$imagename; move_uploaded_file($source, $target); $imagepath = $imagename; $save = "images/gallery/" . $imagepath; //This is the new file $file = "images/gallery/" . $imagepath; //This is the original file list($width, $height) = getimagesize($file) ; $modwidth = 840; $diff = $width / $modwidth; $modheight = $height / $diff; $tn = imagecreatetruecolor($modwidth, $modheight) ; $imgwidth = $modwidth; $imgheight = $modheight; $image = imagecreatefromjpeg($file) ; imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; imagejpeg($tn, $save, 100) ; $save = "images/gallery/sml_" . $imagepath; //This is the new file $file = "images/gallery/" . $imagepath; //This is the original file list($width, $height) = getimagesize($file) ; $modwidth = 80; $diff = $width / $modwidth; $modheight = $height / $diff; $tn = imagecreatetruecolor($modwidth, $modheight) ; $image = imagecreatefromjpeg($file) ; imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; imagejpeg($tn, $save, 100) ; /*MYSQL INSERTION*/ $imgpath = $imagepath; $thumbpath = "sml_". $imagepath; $text_caption = "text_".$i; $caption = $_POST[$text_caption]; if(empty($caption)) { $caption = 'No info'; } mysql_query("INSERT INTO images (imgpath, thumbpath, caption, imgwidth, imgheight) VALUES ('$imgpath', '$thumbpath', '$caption', '$imgwidth', '$imgheight')") or die(mysql_error()); $i++; } } else { print "Error: No image<br />"; $i++; } } header("location: admin.php?p=upload&succes=Alle Billeder blev uploaded."); } ?> Alternativly: http://paste-it.net/public/l3023f6/
  10. is it vital you have it in the query? else you could just pop it in a $var instead.
  11. Is it important that you have a new table for each day? Else you could just have a database like this: //The table name is just what I could think of, add your own. CREATE TABLE IF NOT EXISTS `Trips` ( `pass` int(1) DEFAULT NULL, `pickup` varchar(200) COLLATE latin1_general_ci DEFAULT NULL, `stop_1` varchar(200) COLLATE latin1_general_ci DEFAULT NULL, `stop_2` varchar(200) COLLATE latin1_general_ci DEFAULT NULL, `stop_3` varchar(200) COLLATE latin1_general_ci DEFAULT NULL, `dropoff` varchar(200) COLLATE latin1_general_ci DEFAULT NULL, `amt_coll` varchar(10) COLLATE latin1_general_ci DEFAULT NULL, `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=144 ; Then each time you have a new entry: mysql_query("INSERT INTO Trips (pass, pickup, stop_1, stop_2, stop_3, dropoff, amt_coll) VALUES ('$pass', '$pickup', '$stop_1', '$stop_2', '$stop_3', '$dropoff', '$amt_coll')"); So instead of making a new table, you put the data into a new row. Now you can acces it like I posted above. If you need to make a new table each time (for whatever reason) I guess you could do it like this: //The function requires that your html-form has month, day, and year filled. function WriteTravel($month, $day, $year) { $month = $_GET['month']; //storing the month in the variable $month $day = $_GET['day']; //storing the day in the variable $day $year = $_GET['year']; //storing the year in the variable $year //if any of the above variables is empty, we return an error. (so we dont get a lookup like 06__09 because the day was left out) if(empty($month) or empty($day) or empty($year)) { print "Error: month, day or year wasn't entered."; die; } //Gather up the month day and year to create the table name, stored in $lookup_date $lookup_date = $month ."_". $day ."_". $year; //SELECT ALL (*) FROM $lookup_date for example: SELECT * FROM 05_05_2009 $result = mysql_query("SELECT * FROM $lookup_date"); //Create an array of the table data for the given date. $row = mysql_fetch_array($result); } Now instead of selecting a row from yourTable it selects the table based on what date you input. Now you can print out the data in the table with $row['pass'], $row['stop_1'], $row['amt_coll'] etc. A little tired so hope I didnt make any mistakes, if you cant get this to work I'll try and have a better look and try it out on my own server - hope it helps If there's any specific parts you dont understand let me know and I'll try and explain them better.
  12. I suppose you dont have a new table for each data-input but instead multiple rows. So the query would look like this: $result=mysql_query("SELECT id,pass,pickup,stop_1,stop_2,stop_3,dropoff,amt_coll FROM yourTable WHERE date='$date' ORDER BY id;"); Would probably look something like this: function WriteTravel($month, $day, $year) { $month = $_GET['month']; $day = $_GET['day']; $year = $_GET['year']; if(empty($month) or empty($day) or empty($year)) { print "Error: month, day or year wasn't entered."; die; } $lookup_date = $month ."_". $day ."_". $year; $result = mysql_query("SELECT * FROM yourTable WHERE date='$lookup_date'"); }
  13. It's probably easier for you if you toy abit with some simple data-pulling and inserting data. Try and look at this: the html: <? //Require the function file so we have acces to the WriteGolf() function. require_once("somefunctionfile.php"); ?> <form method='post' action='somefunctionfile.php'> Tournament: <input type='text' name='tournament' /><br /> Points: <input type='text' name='points' /><br /> <input type='submit' name='submit' /> </form> <br /><br /><br /> <? //Writing out the data-base information. GolfWrite(); ?> Then the php(the file: somefunctionfile.php): if($_POST['submit']) { //Check if either of the form-fields were empty, if any are, then return with an error message and kill the script. if(empty($_POST['tournament']) or empty($_POST['points'])) { print "Error: either the tournament and/or point field was empty."; die; } //Set 2 variables from the posted information $tournament = $_POST['tournament']; $points = $_POST['points']; //Insert the typed information into your database mysql_query("INSERT INTO yourTable (tournament, points) VALUES ('$tournaments', '$points')"); header("location: index.php"); } function GolfWrite() { //Pull the data out from the database $q = mysql_query("SELECT * FROM yourTable"); //Here we have a while loop - while we have rows within the array of $q print the information below while($row = mysql_fetch_array($q)) { //Printing out the data print "In Tournament: <b>". $row['tournament'] ."</b> - Boss' son got: <b>". $row['points'] ."</b> points<br />"; } } probably better or easier ways to do it, but this should give you the basic idea of how input/output from a database works.
  14. Last one did it, thankyou alot guys
  15. Ok, gonna try and take the example from what I am working on the ORDER BY id, age - didn't quite work. I have a table: id, player_rank, player_roll lets say I have 30 entries here, 30 different players. Firstly, I want them listed by the player_rank DESC so ie: 10 players with the rank 1-10 ORDER BY player_rank DESC 10 9 8 7 6 5 4 3 2 1 The player_rank is changeing upon certain events, the important thing is that they can be equal, which is why I have player_roll aswell, lets say we have these 10 players with these player_rank values and player_roll values player_rank(player_roll) ----------------------- 10(58) 9(43) 9(97) 7(100) 6(23) 6(84) 3(41) 2(95) 2(13) 1(1) Now thats ordered by the player_rank allright, but I want it to order the player_roll where it's equal, again DESC, so it should look like this: player_rank(player_roll) ----------------------- 10(58) 9(97) 9(43) 7(100) 6(84)--\ -----84 is higher than 23, hence the ORDER BY player_roll DESC puts it first. 6(23)--/ 3(41) 2(95) 2(13) 1(1) Much like Maq suggested (I think), cheers for all the help so far it's superb that a hat like me can get a hand sometimes
  16. This may have a rather simple solution but seeing I've been coding for the good part of 2 days of 10 hour sessions I've roasted my brain. Basicly I have a table with 2 data-lables. for simplicity I'll make a simple illustration. someTable id, name, age some simple data (id, name, age): 1, Dennis, 24 2, Peter, 13 3, Hans, 49 4, Andy, 23 5, Marcus, 29 6, JohnDoe, 58 Now I want my output to be sorted by the 'id': mysql_query("SELECT * FROM someTable ORDER BY id"); further I want my ORDERED output sorted by age, how do I do that? Hope you get it, pretty smashed atm, so might be a poor example.
×
×
  • 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.