PHP - Bug In Php? Pretty Certain It Is.
I am getting Use of undefined constant id - assumed 'id' with this:
$query = "SELECT * FROM products WHERE id = ".mysql_real_escape_string($_POST[id]).""; That seems pretty bull shit to me. Is this a bug in PHP? Similar TutorialsI would like to take the following xml file, and convert it to a nice array. Is there any functions to do this or any suggestions how to do this? xml file: Code: [Select] <?xml version="1.0" encoding="UTF-8"?> <users> <instructions> <phpfile>users.php</phpfile> </instructions> <users action="multiple"> <user> <name>root</name> <password>5f4dcc3b5aa765d61d8327deb882cf99</password> <permissions> <access>CREATE_USR</access> </permissions> </user> <users> </users> Desired array: Code: [Select] Array ( [instructions] => Array ( [phpfile] => users.php ) [users] => Array ( [0] => Array ( [name] => root [password] => 5f4dcc3b5aa765d61d8327deb882cf99 [permissions] => Array( [access] => CREATE_USR ) ) ) ) I'm in the process of building a network for investors and entrepreneurs. When a user logs in, they are directed to profile.php where they can have access to their profile information. If you go into the address bar and type in network.jasonbiondo.com/jasonbiondo you will not be able to view my profile, but if you type in http://network.jasonbiondo.com/profile. ... asonbiondo , you will be able to see my profile. I want to make it cleaner so that you can access profiles by going directly to network.jasonbiondo.com/jasonbiondo . I think my .htaccess file is causing the problem. I have listed my code below. .htaccess Code: [Select] Options +FollowSymLinks RewriteEngine On RewriteCond %{SCRIPT_FILENAME} !-d RewriteCond %{SCRIPT_FILENAME} !-f RewriteRule ^(.*)$ ./profile.php?u=$1 [NC] you can see it at the link below on my site. The generated thumbnails look terrible but I don't know why. Any help greatly appreciated. thank you . (code below of upload script and viewgallery script) http://ealike.com/gallery/viewgallery.php?cid=1 upload script Code: [Select] <?php include("config.inc.php"); // initialization $result_final = ""; $counter = 0; // List of our known photo types $known_photo_types = array( 'image/pjpeg' => 'jpg', 'image/jpeg' => 'jpg', 'image/gif' => 'gif', 'image/bmp' => 'bmp', 'image/x-png' => 'png' ); // GD Function List $gd_function_suffix = array( 'image/pjpeg' => 'JPEG', 'image/jpeg' => 'JPEG', 'image/gif' => 'GIF', 'image/bmp' => 'WBMP', 'image/x-png' => 'PNG' ); // Fetch the photo array sent by preupload.php $photos_uploaded = $_FILES['photo_filename']; // Fetch the photo caption array $photo_caption = $_POST['photo_caption']; while( $counter <= count($photos_uploaded) ) { if($photos_uploaded['size'][$counter] > 0) { if(!array_key_exists($photos_uploaded['type'][$counter], $known_photo_types)) { $result_final .= "File ".($counter+1)." is not a photo<br />"; } else { mysql_query( "INSERT INTO gallery_photos(`photo_filename`, `photo_caption`, `photo_category`) VALUES('0', '".addslashes($photo_caption[$counter])."', '".addslashes($_POST['category'])."')" ); $new_id = mysql_insert_id(); $filetype = $photos_uploaded['type'][$counter]; $extention = $known_photo_types[$filetype]; $filename = $new_id.".".$extention; mysql_query( "UPDATE gallery_photos SET photo_filename='".addslashes($filename)."' WHERE photo_id='".addslashes($new_id)."'" ); // Store the orignal file copy($photos_uploaded['tmp_name'][$counter], $images_dir."/".$filename); // Let's get the Thumbnail size $size = GetImageSize( $images_dir."/".$filename ); if($size[0] > $size[1]) { $thumbnail_width = 100; $thumbnail_height = (int)(100 * $size[1] / $size[0]); } else { $thumbnail_width = (int)(100 * $size[0] / $size[1]); $thumbnail_height = 100; } // Build Thumbnail with GD 1.x.x, you can use the other described methods too $function_suffix = $gd_function_suffix[$filetype]; $function_to_read = "ImageCreateFrom".$function_suffix; $function_to_write = "Image".$function_suffix; // Read the source file $source_handle = $function_to_read ( $images_dir."/".$filename ); if($source_handle) { // Let's create an blank image for the thumbnail $destination_handle = ImageCreate ( $thumbnail_width, $thumbnail_height ); // Now we resize it ImageCopyResized( $destination_handle, $source_handle, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1] ); } // Let's save the thumbnail $function_to_write( $destination_handle, $images_dir."/tb_".$filename ); ImageDestroy($destination_handle ); // $result_final .= "<img src='".$images_dir. "/tb_".$filename."' /> File ".($counter+1)." Added<br />"; } } $counter++; } // Print Result echo <<<__HTML_END <html> <head> <title>Photos uploaded</title> </head> <body> $result_final </body> </html> __HTML_END; ?> DISPLAY THUMBNAILS AND IMAGES SCRIPT Code: [Select] <?php include("config.inc.php"); // initialization $result_array = array(); $counter = 0; $cid = (int)($_GET['cid']); $pid = (int)($_GET['pid']); // Category Listing if( empty($cid) && empty($pid) ) { $number_of_categories_in_row = 4; $result = mysql_query( "SELECT c.category_id,c.category_name,COUNT(photo_id) FROM gallery_category as c LEFT JOIN gallery_photos as p ON p.photo_category = c.category_id GROUP BY c.category_id" ); while( $row = mysql_fetch_array( $result ) ) { $result_array[] = "<a href='viewgallery.php?cid=".$row[0]."'>".$row[1]."</a> "."(".$row[2].")"; } mysql_free_result( $result ); $result_final = "<tr>\n"; foreach($result_array as $category_link) { if($counter == $number_of_categories_in_row) { $counter = 1; $result_final .= "\n</tr>\n<tr>\n"; } else $counter++; $result_final .= "\t<td>".$category_link."</td>\n"; } if($counter) { if($number_of_categories_in_row-$counter) $result_final .= "\t<td colspan='".($number_of_categories_in_row-$counter)."'> </td>\n"; $result_final .= "</tr>"; } } // Thumbnail Listing else if( $cid && empty( $pid ) ) { $number_of_thumbs_in_row = 5; $result = mysql_query( "SELECT photo_id,photo_caption,photo_filename FROM gallery_photos WHERE photo_category='".addslashes($cid)."'" ); $nr = mysql_num_rows( $result ); if( empty( $nr ) ) { $result_final = "\t<tr><td>No Category found</td></tr>\n"; } else { while( $row = mysql_fetch_array( $result ) ) { $result_array[] = "<a href='viewgallery.php?cid=$cid&pid=".$row[0]."'><img src='".$images_dir."/tb_".$row[2]."' border='0' alt='".$row[1]."' /></a>"; } mysql_free_result( $result ); $result_final = "<tr>\n"; foreach($result_array as $thumbnail_link) { if($counter == $number_of_thumbs_in_row) { $counter = 1; $result_final .= "\n</tr>\n<tr>\n"; } else $counter++; $result_final .= "\t<td>".$thumbnail_link."</td>\n"; } if($counter) { if($number_of_photos_in_row-$counter) $result_final .= "\t<td colspan='".($number_of_photos_in_row-$counter)."'> </td>\n"; $result_final .= "</tr>"; } } } // Full Size View of Photo else if( $pid ) { $result = mysql_query( "SELECT photo_caption,photo_filename FROM gallery_photos WHERE photo_id='".addslashes($pid)."'" ); list($photo_caption, $photo_filename) = mysql_fetch_array( $result ); $nr = mysql_num_rows( $result ); mysql_free_result( $result ); if( empty( $nr ) ) { $result_final = "\t<tr><td>No Photo found</td></tr>\n"; } else { $result = mysql_query( "SELECT category_name FROM gallery_category WHERE category_id='".addslashes($cid)."'" ); list($category_name) = mysql_fetch_array( $result ); mysql_free_result( $result ); $result_final .= "<tr>\n\t<td> <a href='viewgallery.php'>Categories</a> > <a href='viewgallery.php?cid=$cid'>$category_name</a></td>\n</tr>\n"; $result_final .= "<tr>\n\t<td align='center'> <br /> <img src='".$images_dir."/".$photo_filename."' border='0' alt='".$photo_caption."' /> <br /> $photo_caption </td> </tr>"; } } // Final Output echo <<<__HTML_END <html> <head> <title>Gallery View</title> </head> <body> <table width='100%' border='0' align='center' style='width: 100%;'> $result_final </table> </body> </html> __HTML_END; ?> http://i.imgur.com/6olCy.png The picture (in the imgur link) pretty much describes what I'm trying to do. As it stands, it would take too long to simply do everything manually with html and css (both of which I'm pretty good at I think), moving all img src and dates from one div tag to another whenever I wanna add something. So yeah, what's the most efficient way to simply submit a picture, and all my other pictures will move? I know some of you guys will say to just learn php, but quite frankly, I don't have the time... and all the php books are ~1000pgs long which seems really daunting. I know, my problem's pretty basic and it'll probably be covered in the first 100pages or so, but still, like I said I don't have that much time for the moment. When I do, however, can you guys recommend me the quickest guide/video series/book to learn practical php? Not the most comprehensive, as the stuff I'm going to be doing's not that complex. One last thing, I've been hearing about CakePHP; what is it? I read that it's an "open source web application framework", but I still have little clue. I'm currently using dreamweaver for all my web stuff (no I do not use the WYSIWYG feature, I just grown accustom to using it). Should I "learn cakephp"? Please answer any one of those questions (particularly the first one). Many thanks and I'll be sure to reply! <?php if (!isset($_GET['register'])) { echo "register stuff here"; } elseif (!isset($_GET['login'])) { echo "login stuff here"; } elseif (!isset($_GET['forgotpassword'])) { echo "password recovery stuff here"; } else { echo "default page stuff here"; } ?> you get what i'm going for, but it kinda messes up this way with different contents on different places? help As a freelancer working alone, I've got to wear all the hats, doing all the front end and back end design and development. So perhaps I'm never really specializing in any one thing, and I tend to miss some good stuff, like MySQL GROUP BY. Doing counts of things using GROUP BY is very handy. I saw somebody doing that in a thread, and adapted it to two things that I was working on in the last week. Sweet!
What other fun MySQL stuff am I missing out on?
I found this online at http://php.net/manual/en/function.time.php: <?php function rel_time($from, $to = null) { $to = (($to === null) ? (time()) : ($to)); $to = ((is_int($to)) ? ($to) : (strtotime($to))); $from = ((is_int($from)) ? ($from) : (strtotime($from))); $units = array ( "year" => 31536000, // seconds in a year (12 months) "month" => 2628000, // seconds in a month (4 weeks) "week" => 604800, // seconds in a week (7 days) "day" => 86400, // seconds in a day (24 hours) "hour" => 3600, // seconds in an hour (60 minutes) "minute" => 60, // seconds in a minute (60 seconds) "second" => 1 // 1 second ); $diff = abs($from - $to); $suffix = (($from > $to) ? ("from now") : ("ago")); foreach($units as $unit => $mult) if($diff >= $mult) { $and = (($mult != 1) ? ("") : ("and ")); $output .= ", ".$and.intval($diff / $mult)." ".$unit.((intval($diff / $mult) == 1) ? ("") : ("s")); $diff -= intval($diff / $mult) * $mult; } $output .= " ".$suffix; $output = substr($output, strlen(", ")); return $output; } ?> ======================= Basically, if you type <?php echo rel_time("March 12, 2011 7:00 PM"); ?>, it will show you the time ago relative to the current time. But there's two problems. 1) The time it sends out is about 5hrs early (the actual time is 7:00pm, I set the rel time to 7:00pm, but the time it displays reads '5 hours ago, 1 minute, and 3 seconds ago'. I'm not much of a coder, but judging by the looks of it, it would appear correct... but apparently not. 2) My second gripe is that the code shows too many increments of date. What I mean is, if if I set the date to over a year ago, it would display the years,months,weeks,days,minutes,seconds. That is way too much increments. I just want it to display seconds, if I submitted it seconds ago, just display minutes if it was displayed minutes ago, and display days if it was submitted days ago. You get the idea. Friends, family, fellow coders, and bored people- can you help a nooblet? Thanks! $username = $loggedInUser->username; // This is the logged in username $time = time(); $makedir = $username.'_'.$time; $var = getcwd(); $var = str_replace('\users', '\imageuploads', $var); $dirlocation = $var."\\".test_directory($username, $mysqli); function test_directory ($username, $mysqli) { $stmt = $mysqli->prepare("SELECT Temp_Directory FROM uc_users WHERE user_name LIKE ?"); $stmt->bind_param("s", $username); $stmt->execute(); $stmt->bind_result($Tempdir); while ($stmt->fetch()){ return $Tempdir; } } if((!empty(test_directory($username, $mysqli))) && is_dir($dirlocation)){ //echo "this is it"; $thedirectory = $dirlocation; } if(empty(test_directory($username, $mysqli))){ //echo "it's not a directory"; $newdir = $var."\\".$makedir; $query = mysqli_query($mysqli, "UPDATE uc_users SET Temp_Directory='$makedir' WHERE user_name='$username'"); if(!$query){ //echo mysqli_error($mysqli); } mkdir($newdir); //security chmod($newdir, 0644); $thedirectory = $newdir; } if(!is_dir($dirlocation) && (!empty(test_directory($username, $mysqli)))){ //echo "third one"; mkdir($dirlocation); chmod($dirlocation, 0644); $thedirectory = $dirlocation; } Ok, so what I'm doing here is testing to see whether a a record exists of the user having a folder in the MySQL database. Then, if it does, make sure that a folder exists at that location. If there is no folder, we create one for the user. If there is already a folder, we leave it alone. This is for image uploads, and $thedirectory, is where we upload images later on in the script. Hope that makes sense. The code seems to work. But how can I improve it and make it more robust? Or should I just leave it alone? Should I return FALSE from the function for better reliability over empty()? Hi all. I was wondering if there was a way (other than retrieving db fields with a query) to show something like "customer_id" as "Customer ID". Is there a functionality of this nature in either MySQL or PHP? I'm aware of preg_replace and such but that would still require two queries; one for the field names and one for the data (right?) |