Jump to content

Iceman512

Members
  • Posts

    81
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

Iceman512's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Hi everyone, I am trying to create a statistics script for my site. I am retrieving the data from a MySQL table and trying to calculate the results. The data displays perfectly as it is, however I have used if and else statements to check it against one another. The page has since become very long and it contains endless if and else queries which hurts me just to look at. I was wondering if there was a more efficient way to accomplish this? Perhaps through a better MySQL query? Here is the bulk of my code: <?php $mon = 0; $tue = 0; $wed = 0; $thu = 0; $fri = 0; $sat = 0; $sun = 0; $jan = 0; $feb = 0; $mar = 0; $apr = 0; $may = 0; $jun = 0; $jul = 0; $aug = 0; $sep = 0; $oct = 0; $nov = 0; $dec = 0; $getstats = mysql_query("SELECT * FROM $table ORDER by id DESC"); while($row = mysql_fetch_array($getstats)) { $stat_id = $row['id']; $stat_ip = $row['ip']; $stat_day = $row['day']; $stat_month = $row['month']; if ($stat_month == '01'){ $jan++; } elseif ($stat_month == '02'){ $feb++; } elseif ($stat_month == '03'){ $mar++; } elseif ($stat_month == '04'){ $apr++; } elseif ($stat_month == '05'){ $may++; } elseif ($stat_month == '06'){ $jun++; } elseif ($stat_month == '07'){ $jul++; } elseif ($stat_month == '08'){ $aug++; } elseif ($stat_month == '09'){ $sep++; } elseif ($stat_month == '10'){ $oct++; } elseif ($stat_month == '11'){ $nov++; } elseif ($stat_month == '12'){ $dec++; } $stat_year = $row['year']; $stat_date = $row['date']; $stat_time = $row['time']; $stat_dotw = $row['dotw']; if ($stat_dotw == 1){ $mon++; } elseif ($stat_dotw == 2){ $tue++; } elseif ($stat_dotw == 3){ $wed++; } elseif ($stat_dotw == 4){ $thu++; } elseif ($stat_dotw == 5){ $fri++; } elseif ($stat_dotw == 6){ $sat++; } elseif ($stat_dotw == 0){ $sun++; } } if ($mon>=$tue && $mon>=$wed && $mon>=$thu && $mon>=$fri && $mon>=$sat && $mon>=$sun){ $topDay = $mon; } elseif ($tue>=$mon && $tue>=$wed && $tue>=$thu && $tue>=$fri && $tue>=$sat && $tue>=$sun){ $topDay = $tue; } elseif ($wed>=$mon && $wed>=$tue && $wed>=$thu && $wed>=$fri && $wed>=$sat && $wed>=$sun){ $topDay = $wed; } elseif ($thu>=$mon && $thu>=$tue && $thu>=$wed && $thu>=$fri && $thu>=$sat && $thu>=$sun){ $topDay = $thu; } elseif ($fri>=$mon && $fri>=$tue && $fri>=$wed && $fri>=$thu && $fri>=$sat && $fri>=$sun){ $topDay = $fri; } elseif ($sat>=$mon && $sat>=$tue && $sat>=$wed && $sat>=$thu && $sat>=$fri && $sat>=$sun){ $topDay = $sat; } elseif ($sun>=$mon && $sun>=$tue && $sun>=$wed && $sun>=$thu && $sun>=$fri && $sun>=$sat){ $topDay = $sun; } ?> Obviously I have done this for months of the year and days of the month as well so you can imagine the length of the if and else's later on! Any suggestions would be appreciated. Well, thanks for taking a look! Regards, Iceman
  2. Hi! Problem solved - that worked perfectly!! Thank u very much Orio and everyone else for looking. Regards, Iceman
  3. Hi everyone! Obviously, I'm a total idiot with arrays. Can someone please tell me why I get the following errors: Warning: chmod() [function.chmod]: No such file or directory in /home/sites/servertestbed.co.uk/public_html/admin/image_new.php on line 113 Warning: getimagesize(../downloads/images/Array) [function.getimagesize]: failed to open stream: No such file or directory in /home/sites/servertestbed.co.uk/public_html/admin/image_new.php on line 115 Warning: Division by zero in /home/sites/servertestbed.co.uk/public_html/admin/image_new.php on line 116 Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /home/sites/servertestbed.co.uk/public_html/admin/image_new.php on line 127 Problem In Creating image when running this code: <?php while(list($key,$value) = each($_FILES[image][name])) { if(!empty($value)){ $size = 144; // the thumbnail height $filedir = '../images/'; // the directory for the original image $thumbdir = '../thumbs/'; // the directory for the thumbnail image $prefix = 'tn_'; // the prefix to be added to the original name $maxfile = '2048000'; // 2MB $mode = '0666'; $userfile_name = $_FILES['image']['name']; $userfile_tmp = $_FILES['image']['tmp_name']; $userfile_size = $_FILES['image']['size']; $userfile_type = $_FILES['image']['type']; if (isset($_FILES['image']['name'])) { $prod_img = $filedir.$userfile_name; $prod_img_thumb = $thumbdir.$prefix.$userfile_name; move_uploaded_file($userfile_tmp, $prod_img); chmod ($prod_img, octdec($mode)); $sizes = getimagesize($prod_img); $aspect_ratio = $sizes[0]/$sizes[1]; if ($sizes[1] <= $size) { $new_height = $sizes[0]; $new_width = $sizes[1]; }else{ $new_width = $size; $new_height = abs($new_width/$aspect_ratio); } $destimg=ImageCreateTrueColor($new_width,$new_height) or die('Problem In Creating image'); $srcimg=ImageCreateFromJPEG($prod_img) or die('Problem In opening Source Image'); ImageCopyResampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) or die('Problem In resizing'); ImageJPEG($destimg,$prod_img_thumb,75) or die('Problem In saving'); imagedestroy($destimg); } echo '<p style="vertical-align:center;"><a href="'.$prod_img.'" target="_blank"> <img src="'.$prod_img_thumb.'" width="'.$new_width.'" height="'.$new_height.'" border="0"> </a> Image uploaded successfully!</p><br />'; } elseif (empty($value)) { echo 'Input field was empty<br />'; } } ?> Here is the form used to submit the images/files: <?php $max_img = 5; // Number of upload fields echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data">'; for($i=1; $i<=$max_img; $i++) { echo 'Image '.$i.' <input type="file" name="image[]" size="45" class="bginput" /><br />'; } echo '<input style="padding:2px 5px;" type="submit" name="newImage" value=" Upload " />'; echo '</form>'; }?> Thank you for any help! Regards, Iceman
  4. Hi there, Yup, that fixed it immediately! Man... I gotta get some sleep. Thank you very much! Regards, Iceman
  5. Hi all, The following script uploads and re-sizes an image and then inserts the relevant information into the database. It works perfectly (the image is uploaded, resized and the data gets submitted to the db), but instead of displaying the 'upload successful' message, I get the following error every time: Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1 Can anyone tell me why? Here the main code for this part: <?php if (isset($_POST['linkNew'])){ require_once 'config.php'; $site = ucwords($_POST['site_name']); $link = preg_replace('/\s+/', '_', $_POST['website']); $date = $_POST['date']; $description = trim($_POST['description']); $sql = mysql_query("INSERT INTO table (image, site, link, date, description, publish) VALUES ('$img_thumb', '$site', '$link', '$date', '$description', 'yes')"); if (!mysql_query($sql,$con)) { exit('Error: ' . mysql_error()); } echo '<p>Your link was saved successfully.</p>'; mysql_close($con); } else { ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data"> <b>Upload Image:</b> <input type="file" name="image" size="45" /><br /> <span style="font-size:10px; color:#666666;">Click <b>browse</b> to select an image</span><br /><br/> <b>Link Name:</b> <input type="text" name="site_name" size="35" /><br /> <b>Website:</b> <input type="text" name="website" size="35" /><br /> <span style="font-size:10px; color:#666666;"><b>Eg: </b>http://www.some_site.com</span><br /><br/> <b>Date:</b> <input type="text" name="date" value="<?php echo date('jS '); echo date('F, Y'); ?>" /><br /><br /> <b>Site Description:</b> <br /> <textarea style="width:99%; height:80px; overflow:visible;" name="description"></textarea> <input style="padding:5px 10px;" type="submit" value=" Submit " name="linkNew" /> </form> <?php } ?> Thank you for any help in advance Regards, Iceman
  6. Hi, I'm sorry, not sure what you mean? Do you have to wait 60 seconds for the page to load or do your php scripts take 60 seconds or longer to execute? I'm sure you are aware that 60 seconds on the internet is a holiday.... a long one! So that's a really long wait. Otherwise, you might want to increase the server's time limit on script execution using ini_set: Check out the related stuff here: http://uk2.php.net/set_time_limit Hope it helps! Regards, Iceman
  7. Hi there, I'm actually a real idiot when it comes to file handling, so forgive me for any dumb statements I may make here! Where have you declared the value for $type? Apart from the function, I cannot see where you have set it and your conditional statements revolve around this variable. Also, can you try echoing your cookie data at the end of the page once the form has been submitted, that way you can confirm that they're not empty. Just a thought, Iceman
  8. Ok, I see. Thanks for the replies. I'll take a look. Regards, Iceman
  9. Hi everyone, Could someone please shed some light on the correct use of brackets { and } in a MySQL query. For example, I understand that the following query is not entirely correct: <?php $sql = "SELECT id, title, date, description, publish FROM $dbtable WHERE title LIKE '$search%' || body LIKE '$search%' && publish='yes' ORDER by id DESC LIMIT '$offset', '$limit'"; ?> Rather, the following should be used but is this actually correct?: <?php $sql = "SELECT id, title, date, description, publish FROM $dbtable WHERE title LIKE '{$search}%' || body LIKE '{$search}%' && publish='yes' ORDER by id DESC LIMIT '{$offset}', '{$limit}'"; ?> I have looked this up on the net, but there seem to be many conflicting opinions. Thanks to anyone who can shed some light. Regards, Iceman
  10. Hi there, I sure ain't no genius, but here goes... MySQL reads apostrophes as part of the query input, so it will try to process them in the query submitted to the database. This often ends with a replacement character such as a question mark, depending on the server settings. Fortunately, you do have many options on what you want to do with them when submitting data to the database. There are several built-in functions within PHP which will either convert apostrophs and other characters into db-friendly characters, add more apostrophe's before or after them to make "doubles" or simply strip them out altogether. Take a look at the following: http://uk.php.net/get_html_translation_table http://uk.php.net/add_slashes You will have to process the data for submission before it goes into your query, like so: <?php $str = $testing['cId']; echo stripslashes($str); $test="INSERT INTO test VALUES ('$str','$somethingelse')"; ?> There are many more functions that can be suited for this task, and I'm sure someone else can suggest more. Hope that helps! Regards, Iceman
  11. Hi, Sorry, I have just spotted a mistake in the code: $i++ should be: $i++; Iceman
  12. Hi there, Ok... give the following a try and see if it works. <select name="select"> <option value=""><b> ----- PLEASE SELECT ONE ----- </b></option> <?php require_once('config.php'); // Contains DB info and connection $i='0' $sql = ("SELECT * FROM table ORDER BY name DESC"); // For speed, only pull the rows you need while($row = mysql_fetch_array( $sql )) { $id = $row['id']; $parent = $row['parent']; $keyword = $row['keyword']; $name = $row['name']; if ($parent = '0' || $parent == '0'){ echo '<option value=""><b>» » '.$name.'</b></option>'; $i++ if ($parent = $i || $parent == $i){ echo '<option value="'.$keyword.'">'.$name.'</option>'; } else { } } } mysql_close($con); // Close the connection as specified in config.php ?> </select> Let's hope so.... Iceman
  13. Hi, It will have to have the following: <?php /* ----- DB CONFIGURATION ----- */ $dbhost = 'host'; $dbuser = 'user'; $dbpass = 'password'; $dbname = 'database'; /* ----- DB CONNECTION ----- */ $con = mysql_connect($dbhost, $dbuser, $dbpass); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db($dbname, $con); ?> Just use it as an include. Iceman
  14. Hi again, Ok.... sorry, I did misunderstand you. Well, let us try the following: <select name="select"> <option value=""><b> ----- PLEASE SELECT ONE ----- </b></option> <?php require_once('config.php'); // Contains DB info and connection $sql = ("SELECT * FROM table ORDER BY name DESC"); // For speed, only pull the rows you need while($row = mysql_fetch_array( $sql )) { $id = $row['id']; $parent = $row['parent']; $keyword = $row['keyword']; $name = $row['name']; if ($parent = '0' || $parent == '0'){ echo '<option value=""><b>» » '.$name.'</b></option>'; } elseif ($parent != '0' || $parent !== '0'){ echo '<option value="'.$keyword.'">'.$name.'</option>'; } else { echo 'An uknown error has occured.'; } } mysql_close($con); // Close the connection as specified in config.php ?> </select> I hope that sets you on the right path. Again, I haven't tested the above code yet. Regards, Iceman
  15. Hi again, The code I have written should order the drop-down list by category as you require. The key to arranging the data by category is here: ... ORDER BY name DESC"); Please copy this modified piece of code to your page and try it.... it should work <select name="select"> <option value=""><b> ----- PLEASE SELECT ONE ----- </b></option> <?php require_once('config.php'); // Contains DB info and connection $sql = ("SELECT * FROM table ORDER BY name DESC"); // For speed, only pull the rows you need while($row = mysql_fetch_array( $sql )) { $id = $row['id']; $parent = $row['parent']; $keyword = $row['keyword']; $name = $row['name']; echo '<option value="'.$keyword.'"><b>'.$name.'</b></option>'; } mysql_close($con); // Close the connection as specified in config.php ?> </select> NOTE: You say that the columns in the DB are id, parent, keyword and name. However in your code, the columns are code, provider_name, id, parent. Is this intentional? Let us know if it works! Iceman
×
×
  • 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.