Psycho
Moderators-
Posts
12,157 -
Joined
-
Last visited
-
Days Won
129
Everything posted by Psycho
-
Why do you need it to be done in one query? Just do the insert into the first table, then get the id using mysql_insert_id(). Then do your Insert into the second table using that id.
-
http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing
-
http://php.net/manual/en/language.types.string.php
-
for($year = $newestYear; $year >= $oldestYear; $year--) { $selected = (isset($birthYear) && $birthYear == $year) ? ' selected="selected"' : ''; echo "<option value='{$year}'{$selected}>{$year}</option>\n"; }
-
Yes, I did. BUt, on second review I also forgot that you don't put quotes around the index names in the HTML form, so it should actually be like this: for($i=0; $i<3; $i++) { echo "<input name=\"names[$i][name]\" />\n"; echo "<input name=\"names[$i][name1]\" />\n"; } Seriously? Please re-read what I said. I told you that by creating the input fields with names in that format the data would ALREADY be in the format you requested and I even provided an example of what that array would look like.
-
Can't Understand Why This Code is'nt Working
Psycho replied to JamesThePanda's topic in PHP Coding Help
Easy - there were no results. One reason my be that you put the field name in single strait quotes. So instead of looking for any records were the field ID has a value in the list, the query is trying to see if the STRING 'ID' is in the list of values. You should create your queries as a string variable so you can echo to the page for debugging purposes. And you can add other code to verify if there were results or not. Also, I don't know what you expect the post value to look like, but you may not get the results you expect based on how it is formatted $ids = $_POST['uid']; $con = mysql_connect("localhost","root",""); mysql_select_db("product", $con); $query = "SELECT `filename`, `title` FROM `product2` WHERE `ID` IN ('$ids')" $result = mysql_query($query); if(!mysql_num_rows($result)) { echo "There were no results"; } else { while($row = mysql_fetch_assoc($result)) { echo "{$row['filename']} {$row['title']}<br>\n"; } } mysql_close($con); -
This is a JavaScript problem, not a PHP problem, moving to the correct forum. Now, to your problem, I don't think that form will ever POST because you have a "return false" in the trigger. But, you should not call your form validation in the submit button because a form can also be submitted by using the enter key if you are not in an input field. the proper way to do it is with the onsubmit trigger of the form. Then the validation function(s) will return a true if validation passes and the form posts, else it returns false and the form doesn't post. But, your current JS code has errors. You are missing a closing paren after the list of conditions at the beginning of the function. Also, you are referencing fields that don't exist (i.e. access_period). Here is the updated HTML portion of your script which should work <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>User Registration Form</title> <script language="javascript"> function validateForm() { var form = document.forms[0]; if (!form.fname.value.length || !form.lname.value.length || !form.email.value.length || !form.phone.value.length || !form.city.value.length || !form.status.selectedIndex || !form.province.value.length || !form.password.value.length || !form.password2.value.length ) { //Validation failed alert("Please fill out all the required fields."); return false; } //Validation passed return true; } </script> </head> <body><center><br /><br /> <pre> <?php print_r($_POST); ?> </pre> <?php echo $error; ?><br /><br /> <form name="registration" method="post" action="./registered.php" onsubmit="return validateForm();""> <table> <tr> <td>First Name:</td> <td><input type="text" name="fname" value="<?php $_SESSION['fname'] ?>" size="30" maxlength="50" /></td> <td>*</td> </tr> <tr> <td>Last Name:</td> <td><input type="text" name="lname" value="<?php $_SESSION['lname'] ?>" size="30" maxlength="50" /></td> <td>*</td> </tr> <tr> <td>E-Mail Address:</td> <td><input type="text" name="email" value="<?php $_SESSION['email'] ?>" size="30" maxlength="50" /></td> <td>*</td> </tr> <tr> <td>Phone:</td> <td><input type="text" name="phone" value="<?php $_SESSION['phone'] ?>" size="30" maxlength="50" /></td> <td>*</td> </tr> <tr> <td>City:</td> <td><input type="text" name="city" value="<?php $_SESSION['city'] ?>" size="30" maxlength="50" /></td> <td>*</td> </tr> <tr> <td>Status:</td> <td> <select name="status" id="status"> <option>Pls. Select One <option value="single">Single <option value="relationship">In a Relationship <option value="complicated">It's Complicated </select> </td> <td>*</td> </tr> <tr> <td>Province:</td> <td><input type="text" name="province" value="<?php echo $_SESSION['province'] ?>" size="30" maxlength="50" /></td> <td>*</td> </tr> <tr> <td>Password:</td> <td><input type="password" name="password" size="30" maxlength="50" /></td> <td>*</td> </tr> <tr> <td>Confirm Password:</td> <td><input type="password" name="password2" size="30" maxlength="50" /></td> <td>*</td> </tr> <tr> <td> <input type="reset" name="Reset" value="Reset"></td> <td> <input type="submit" name="Submit" value="Submit" ></td> </tr> </table> </form> </center> <script language="javascript"> // set the selection box values... form.status.selectedIndex = parseInt("<?php echo $_SESSION['status'] ?>"); </script> </body> </html>
-
This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=356816.0
-
Typically you want to provide the user a format that they are to follow when entering the date and/or provide a popup calendar. So, what format do you expect the user to enter the date? As long as you use a standard'ish' format you can use strtotime() to convert to a format that can be used in PHP then translated into a proper format for the database. $adddob = strtotime($_POST['dob']); $sql_date = date('Y-m-d', $adddob); Of course you definitely want to add some validation to this.
-
While inside while, needs to be something different
Psycho replied to HCProfessionals's topic in PHP Coding Help
I've said it three time and I'm not going to say it again. But, not that you "seem" to have provided some more information I will attempt to help further. I think you are saying that the products may have multiple images in the product_images table, but you only want the thumbnail image. Are there multiple thumbnails per product? If so, do you only want one? The problem you were likely having in your previous query was probably due to some products having no thumb or multiple thumbs. But, you didn't provide any information as to your DB layout. But, this should be able to be resolved by putting the thumb check in the correct place and possibly using a GROUP BY function. I believe I also stated you should not be using '*' in your select query and should instead list the applicable fields. No matter - You still only need ONE query.. If you are running queries in a loop you are doing it wrong. The following query will include the thumbnail image in the results for each product. Even if there are multiple thumbnails you will only get one per product. SELECT p.product_price, p.product_id, i.image_name FROM products AS p LEFT JOIN product_images AS i ON p.product_id = i.product_id AND i.thumb='1' WHERE p.product_featured='1' AND p.product_active='1' GROUP BY p.product_id Now this may not be the correct query, but it's the best I can come up with based on the information you have provided. But, this is definitely the direction you want to go in. -
Then just change your field names so the data will already be in the format you need. <form> <input name="names[0]['name']" /> <input name="names[0]['name1']" /> <input name="names[1]['name']" /> <input name="names[1]['name1']" /> <input name="names[2]['name']" /> <input name="names[2]['name1']" /> </form> Although it would be best to create the fields using PHP code for($i=0; $i<3; $i++) { echo "<input name=\"names[0]['name']\" />\n"; echo "<input name=\"names[0]['name1']\" />\n"; } The submitted data will be in this format Array ( [0] => Array ( [name] => name from first set [name1] => name1 from first set ) [1] => Array ( [name] => name from second set [name1] => name1 from second set ) [2] => Array ( [name] => name from third set [name1] => name1 from third set ) )
-
While inside while, needs to be something different
Psycho replied to HCProfessionals's topic in PHP Coding Help
Did you LOOK at the code I provided? Does it provide the results you were looking for? If not, how are they different. If you even gave the code a cursory glance you will notice that I used the results from the initial query to get the product information and the image information. I will say this a third time (between the last post and this one): Running that second query inside the while() loop is completely unnecessary and not advised. You should never run queries in a loop Yes, I did. -
While inside while, needs to be something different
Psycho replied to HCProfessionals's topic in PHP Coding Help
Wasn't this already covered in your earlier post? There is no need to run the 2nd query inside the first while loop. And, you should NEVER run queries within loops - it will lead to significant performance problems. You are already JOINing the product_images table on the products table. So, you already have the data you need in the first query results. Take another look at the code I last posted in your earlier thread. -
Mass MySQL downloading - What's the most efficient way through PHP?
Psycho replied to soma56's topic in PHP Coding Help
EDIT: PFMaBiSmAd already raised some of the same issues, but I'm going to post all of this as is. Well, I would suggest using fputcsv() to generate the content instead of building your own csv writer. Additionally, I would write the contents to a temp file and then send that to the user - instead of compiling a 'dynamic' file. That way it all doesn't have to be held in memory during processing (I think) I see other inefficiencies as well. For example, you use this to get the total number of rows //determine table total $what_is_the_total = "SELECT * FROM $current_table"; $result_total = mysql_query($what_is_the_total); $num_rows = mysql_num_rows($result_total); That is a huge waste since the DB has to return all those rows. Rather, just query for the count of rows and get one record back //determine table total $query = "SELECT COUNT(id) FROM $current_table"; $result = mysql_query($query); $num_rows = mysql_result($result, 0); Also, don't use '*' in your select statements unless you really need all the columns in the result. Again, that is a waste of resources. I see that you are looping through each field in each record. You can process a whole row at once with fputcsv(). To get the names have a trigger for the first record and use array_keys() to get the field names to generate the first line. This a very quick/rough rewrite, but it should be much more efficient ini_set('memory_limit', '512M'); //Connect to Database require("db_connect.php"); $current_table = trim($_GET['current_table']); $current_table_results = mysql_real_escape_string($_GET['current_table']."_results"); //$start_id = $_GET['start_id']; //$end_id = $_GET['end_id']; //Create and run query $query = "SELECT * FROM $current_table_results"; $result = mysql_query($query) or die("Error executing query: ".mysql_error()); //Create output file object $fileObj = fopen("{$current_table}.csv", 'w'); $header = true; while($row = mysql_fetch_assoc($result)) { if($header==true) { fputcsv($fileObj, array_keys($fields)); $header = false; } fputcsv($fileObj, $fields); } //Output and delete temp file $file_content = file_get_contents($fileObj); unlink($fileObj); header("Content-type: text/csv"); header("Content-Disposition: attachment; filename=$current_table-$start_id-$end_id.csv"); echo $file_content; exit; -
Multiple While's, Single Query - Is there a way?
Psycho replied to HCProfessionals's topic in PHP Coding Help
DO NOT RUN QUERIES WITHIN LOOPS! You can get all the data you need with a single query. //Create/run query $query = "SELECT products.product_price, products.product_id, product_images.image_name FROM products LEFT JOIN product_images ON products.product_id=product_images.product_id WHERE products.product_featured='1' AND products.product_active='1' AND thumb='1'": $result = mysql_query($query); //Vars to hold content $html_1 = ''; $html_2 = ''; $index = 0; //Process data into the content vars while($row = mysql_fetch_assoc($result)) { $imgClass = ($index==0) ? 'home-slider-photo preload' : 'home-slider-photo preload home-slider-photo-unsel'; $divClass = ($index==0) ? 'home-slider-photo-price' : 'home-slider-photo-price home-slider-photo-price-unsel'; $html_1 .= "\n<img id=\"home-slider-photo-{$index}\" class=\"{$imgClass}\" src=\"/includes/getimage.php?img={$row['image_name']}&w=370&h=370\" alt=\"\" />"; $html_2 .= "\n<div id=\"home-slider-photo-price-{$index}\" class=\"{$divClass}\">\n<span>only</span>${$row['product_price']}\n</div>"; $index++; } echo $html_1; echo $html_2; echo "</div>"; -
Mass MySQL downloading - What's the most efficient way through PHP?
Psycho replied to soma56's topic in PHP Coding Help
How are you doing it now? Code please. -
Multiply Array Values from Multiple Arrays
Psycho replied to unemployment's topic in PHP Coding Help
The OP stated that he/she was only providing two as an example. That could be edited for multiple arrays, but not if the number is dynamic. Plus, if there are keys in array 2 that don't appear in array 1, they would be missed (although that may not be an issue). @op: You are getting these values from somewhere (a DB perhaps?). There is a very good chance you can do what you need when you are extracting the records and are making this more difficult than you need to. But, with what you have stated I have another approach that should work no matter how many arrays you have or which keys each array does or does not have $arry1 = array(1, 5); $arry2 = array(2, 6); //Put all data arrays into parent array $arraysList = array($arry1, $arry2); //Put values into new combined mufti-dimensional array //With the values in sub-arrays by common keys $masterArray = array(); foreach($arraysList as $array) { foreach($array as $key => $value) { $masterArray[$key][] = $value; } } //Calculate product of each value set and add to total $total = 0; foreach($masterArray as &$valueSet) { $total += array_product($valueSet); } echo $total; -
Help with multi-dimensional associative array
Psycho replied to arenaninja's topic in PHP Coding Help
@jesirose: Welcome back! Haven't seen you in forever. @op I would suggest NOT creating a new Table for each set of "instType". You can certainly do that, but it would complicate the output because you need to close each table. I could take the time to explain why it would be more difficult, but that'd take too long. I'll show you an example without it and if you still feel it's necessary you can try and modify as needed. Anyway, what you basically need to do is use a couple of "flag" variables to detect when there is a change in one of the primary values. Although, typically, you would have the same value types in a row. I've never seen the need to build logic where the row is dynamically determined. Are you going to have different sets of instrumentIDs for each row or are you always going to have the same ones? If the former, the output will be really chaotic, if the latter then you don't want the logic you've asked for. Anyway, based upon what you asked (which may not really be what you need) some sample code could look like this (not tested, sot here might be a couple errors) $result = mysql_query($query); $typeFlag = false; $paymentFlag = false; //Open table echo "<table>\n"; //Process records while($row = mysql_fetch_assoc($result)) { //Create new header row if new type if($typeFlag != $row['instType']) { //Close prev data row if open if($paymentFlag != false) { echo "</tr>\n"; $paymentFlag = false; } //Display type header echo "<tr><th>{$row['typeName']}</th><tr>\n"; $typeFlag = $row['instType']; } //Create data row if new payment if($paymentFlag != $row['paymentID']) { //Close prev data row if open if($paymentFlag != false) { echo "</tr>\n"; } echo "<tr>\n"; $paymentFlag = $row['paymentID'] } echo "<td>{$row['instrumentName']}</td>\n"; } //Close table echo "</table>\n"; -
Which is basically the exact same solution I gave him in this thread: http://www.phpfreaks.com/forums/index.php?topic=356316.msg1683820#msg1683820 But, he said he "solved" it using the same code he provided above and I questioned the same problems you pointed out. << Psycho adds newphpcoder to his ignore list >>
-
Not really. You can have a single user that uses multiple IPs (work/home or gets a different IP from his provider) or you have have multiple users behind the same IP using NAT. So, using the IP address is not guaranteed. Using cookie is a little better, IMHO. But, you would get multiple hits if the same user views the same content on different machines. Also, a user could delete their cookies and that would incur an additional hit if they viewed the same content after deleting the cookie. And then there are those that set their browsers to not accept cookies which would generate a hit on every view since you wouldn't be able to set a cookie to exclude them in the future. As scootstah a login system would probably be the most reliable, but even that isn't foolproof. A user could create another account and view the same content again, which would trigger another hit. It all depends on exactly what you ar trying to ascertain from the data that should guide you in deciding the best method.
-
And to find out why your query was failing and the one chriscloyd posted will work, you should look here: http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html
-
Enabling PHP short_open_tag
Psycho replied to kennethk's topic in PHP Installation and Configuration
Look to see if there is an updated version of the script. If not, the script probably isn't worth using anyway. I'm not an SEO expert, but I would think that there are enough changes over time that what was a good script 2 years ago probably isn't such a good script today. And, if the script was written recently, I wouldn't rely upon it if the author wasn't aware that using short tags is not support by default any more. -
I would agree that - in most situations - you should not use the global keyword to make a variable global. But, defining constants - which will be global - is not a bad practice. And, many variables are global by default. So, I would not say to ignore the 'word' global. When a user makes a request for a 'page' there may be any number of scripts that are executed to generate the page. Once a constant is defined any additional code that is run can reference that constant. It does not mean that you can define a constant on one page load and then have that constant available in subsequent page loads. It would need to be defined on any page request that you need it - and it does not retain it's value from previous page loads. The easiest way to understand how includes work is this. Imagine that wherever there is an include (or require) that the system is copying the code from the include file and pasting it into the file that included it. Anything that occured in earlier files will affect any code in included files just as if they were in the same script. Depends on what you mean. If that script/file is included() during the same execution that the function was run then, yes, it would be available. If it is a different page load, then no. But, as muddy_funster stated, this is considered poor practice and I agree. You should read through this page in the manual: http://us3.php.net/manual/en/language.variables.scope.php