Psycho
Moderators-
Posts
12,157 -
Joined
-
Last visited
-
Days Won
129
Everything posted by Psycho
-
There is absolutely no need to create a function for every link! You can just echo the function BEFORE the loop. The only reason I didn't give you code for that was because this process is much more difficult than it should be to begin with. And, as I stated, using "hidden" fields adds absolutely NO security.
-
Using a form is adding no more security than allowing the ID to be put on the query string. It is a trivial task to modify the data being sent in a form (even in hidden and select fields). That is why you should ALWAYS validate data coming from the user (POST, GET, COOKIE, etc.). So, your page to display the folder contents should verify that the ID belongs to the user which you already know based upon session data, right? You should probably be storing the user ID in session rather than username though.
-
Have you looked at the actual source code created? Have you used any error console within your browser to see what javascript errors are being thrown? If you had done either I'm sure you'd see the problem. Plus, I can tell you're doign it wrong because you are using global and you are using mysql_real_escape_string() to escape data for the HTML page (?). You have a while loop that is generating content. Part of that content is a javascript function. You cannot have two functions with the same name in your page! And, the way you are doing this is way more complicated than it should be. You should 1. Use a common HTML hyperlink and send the folder ID as part of the query string 2. Change your viewfolder script to use the folder ID instead of the folder name global $user; $username = $user->name; $listquery = mysql_query("SELECT ID, foldername FROM folders WHERE username='$username'"); while ($rows = mysql_fetch_array($listquery)) { $foldername = htmlentities($foldername); echo "<a href='viewfolder.php?if={$rows['ID']}'>{$foldername}</a><br>\n"; }
-
Absolutely. I tend to use the initial check to see if the values are different (as in my example) because it let's me easily add additional sorting criteria.So, if the 'stamp' values were the same there could be some additional logic to then sort on field#2, then field#3, etc. So, using your more elegant solution . . . function customSort($a, $ B) { if($a['stamp'] != $b['stamp']) { return $b['stamp'] - $a['stamp']; } if($a['field2'] != $b['field2']) { return $b['field2'] - $a['field2']; } if($a['field3'] != $b['field3']) { return $b['field3'] - $a['field3']; } return 0; }
-
** Marking solved **
-
So append the strings to a variable rather than echo them. <?php $tableData = ''; foreach($feat as $name => $dataAry) { $tableData .= "<tr>\n"; $tableData .= "<td>{$name}</td>\n"; foreach(explode(':', $dataAry) as $data) { $tableData .= "<td>{$data}</td>\n"; } $tableData .= "</tr>\n"; } ?> <html> <body> <table border='1'> <?php echo $tableData; ?> </table> </body> </html>
-
Str_Replace - Remove Everything If It Has A : In It
Psycho replied to strago's topic in PHP Coding Help
Why don't you show the input and the desired output. You code doesn't make sense. The first line replaces a null string with a null string and adds a right paren to the end of the string. The second line adds a left paren to the beginning of the string. The third line replaces a left and right paren with an empty string. I assume this last step is to remove the value entirely if the originating input was empty. You could have replaces those three lines with $source = (!empty($db_source)) ? "({$db_source})" : ''; But, your request is not even clear. What do you mean by "it all get's competly [sic] removed"? If you mean the value for one $db_source then you could simply check if there's a colon $source = (!empty($db_source) && strpos($db_source, ':') === false) ? "({$db_source})" : ''; -
function customSort($a, $ B) { if($a['stamp'] != $b['stamp']) { return ($a['stamp'] < $b['stamp']) ? -1 : 1; } return 0; } //use usort() to sort array with custom function usort($YOURARRAY, 'customSort'); //The original array variable is now sorted
-
I am really not following your code, so not sure what you are really trying to accomplish. But, taking your original request, start with this <?php $feat = array( "feat1" => "nocolon1:colog1", "feat2" => "nocolon2:colog2", "feat3" => "nocolon3:colog3", "feat4" => "nocolon4:colog4", ); echo "<table border='1'>\n"; foreach($feat as $name => $dataAry) { echo "<tr>\n"; echo "<td>{$name}</td>\n"; foreach(explode(':', $dataAry) as $data) { echo "<td>{$data}</td>\n"; } echo "</tr>\n"; } echo "</table>\n"; ?>
-
Yeah, you need to show a sample of the data in the array and give a better explanation of what you are trying to achieve (such as an image). Kinf of hard to know what doesn't "fit" from your description.
-
To expand on PFMaBiSAd's response. You have a many-to-one relationship which necessitates two tables. Since you are inserting into a table called "folders" I assume you already have a users table (if not you need one). Also, your users table should have an auto-increment ID field for the purposes of "relating" data between tables (thus the term relational database). So, I would expect to see two tables with at least these fields users user_id, username, etc. folders folder_id, user_id, foldername Now, for every folder that a user has there will be a unique record in the folders table. A user can have no folders, 1 folder, or many folders. If you want to limit the number of folders a user has you can take care of that in the code.
-
Cant Figure Out A Function That Displays Instances Of Characters.
Psycho replied to CallMeDiz's topic in PHP Coding Help
You just created a function that does exactly what an existing function already does. That is pointless. You could just do . . . print substr_count($mystring, $mychar); . . . instead of making a function. The reason to build your own function is to build a repeatable process to do something that doesn't already exist. -
Cant Figure Out A Function That Displays Instances Of Characters.
Psycho replied to CallMeDiz's topic in PHP Coding Help
substr_count() -
If you are seeing actual "\r\n" in the database values then either 1) the user is entering those actual characters into the form and it is correct or 2) You are somehow injecting those characters into the values before you insert them into the database. mysqli_real_escape_string() will properly escape linebreaks to "\r\n", but that is so they will be properly inserted into the DB as linebreaks. What you are seeing is what would happen if you were escaping the input twice.
-
NO, that is not a single-dimensional array. It is a multidimensional array array where each subarray has a single value. This is a single dimensional array Array ( [jazzman_1@gmail.com] => jazzman [slippers@gmail.com] => slippers [jazz@gmail.com] => jazz [jazzman_2@gmail.com] => jazzman [jazzman_3@gmail.com] => jazzman [jazzman_4@gmail.com] => jazzman [jazzman_5@gmail.com] => jazzman [jazzman_6@gmail.com] => jazzman [jazzman_7@gmail.com] => jazzman [jazzman_8@gmail.com] => jazzman [jazzman_9@gmail.com] => jazzman [jazzman_10@gmail.com] => jazzman [jazzman_11@gmail.com] => jazzman [jazzman_12@gmail.com] => jazzman ) The problem is how this array is being created while ($row = mysql_fetch_assoc($result)) { $menu_array[$row['id']] = array($row['email']=>$row['name']); } The array() after the equal sign is creating a subarray! It should just be this while ($row = mysql_fetch_assoc($result)) { $menu_array[$row['email']] = $row['name']; } To get the array into "chunks" of three you could implement logic in the creation process above, or just use array_chunk() after the single dimensional array is created while ($row = mysql_fetch_assoc($result)) { $menu_array[$row['id']] = array($row['email']=>$row['name']); } $menu_array = array_chunk($menu_array, 3, true);
-
This really isn't the appropriate medium to address this, but I can't let that stand. I never made a disparaging comment towards you in this thread. The "little jabs" you are referring to are your own words that you used towards me. If you are taking offense at those words being used, then perhaps you should not have used them towards someone. You were wrong from the very start of this thread and at every step along the way. Yet, you decided to make a disparaging remark towards me about my ability to understand "basic language". Rather, you were the one that didn't understand the "basic language" in the request which everyone else seemed to understand. Then, after all the defense of your position, when it is obviously wrong, you don't have the decency to at least say "oh yeah I goofed that one" or provide an apology, yet you have the audacity to say you respect me.
-
That doesn't make sense. \r\n are the escaped characters for line breaks. You shouldn't "see" those literal characters in the output. I don't believe it would be likely that you are converting real linebreaks to their escaped character codes. So, it is probably more likely that you are inserting those in the code somewhere as literal characters. Do you see those characters in the database? If so, the problem occurs before you save the data. If you do not see those in the database, then the problem occurs after you retrieve the data from the database. I think you need to go back and look at each step where you transform the data. Do an echo before and after each step. Don't just rely upon what you see in the browser also check the HTML source. Again, you should never actually "see" \r\n in the output. Those should result in a line break in the source.
-
My pleasure. I do pretty good for someone that isn't good at "understanding basic language".
-
Well, don't add line breaks. In a text area a line break is represented by a real line break. echo "This line <br> does not have a real line break"; echo "This line does have a real line break"; echo "This line \n also has a real line break"; It doesn't look like you are removing any explicit line breaks before inserting into the DB. Try displaying the text w/o using the addbreaks() function.
-
You are storing the result of the query in the variable $result. However, the query is failing, so $result is set to the Boolean FALSE value. On line 181 you then try to "use" the result of the query by extracting a row from the result while($row = mysql_fetch_assoc($result)) As stated above, since the query failed $result does not contain a resource (pointer) to a query result. You should always check your queries to see if they fail. If they do you should output the MySQL error and the actual query to the page for debugging purposes. In this case I see a few problems. 1. I'm not certain, but I don't think != is valid in MySQL. You should use <> 2. This is not valid if($result > 0){ I'm pretty sure you want to check if the result contained more than 0 ROWS. As stated above $result will contain a resource to the result set - it is not the count of the records returned. Instead you would do this: if(mysql_num_rows($result) > 0){ Use the following $sql = "SELECT * FROM members WHERE confirmation = '1' AND type <> 'Admin' AND image <> 'uploads/propic.jpg' ORDER BY RAND() LIMIT 9"; $result = mysql_query($sql); if(!$result) { echo "The query failed. Query: {$sql}<br>Error: " . mysql_error(); } elseif(!mysql_num_rows($result)) { echo "There were no records returned."; } else { //Display the results $first = $row['image'];
-
Let's look at the OP's post and break it down. Besides the greeting and the closing smiley there were two sentences in his post. Here is the first sentence That ends in a period. That is a statement, not a question. It could be information that is pertinent to his problem to come later or perhaps he was just proud of the fact that he built that and wanted us to be aware. But it is not a question asking for a response Here is the 2nd sentence Now that sentence is followed by a question mark (also called an interrogation point). Now, that is a question. I don't read anything in that question about validation. But, if you go back to the first sentence it all makes sense. He is posting the form to itself so he can do the validation and repopulate the form if validation fails. But, he is having problems figuring out how to repopulate/reset the select list to the value the user had in their form submission. Now, let's look at your first response that you say answers his question Considering that this is the PHP forum and not the HTML forum, I think that is not an appropriate answer. I believe it is obvious the was asking about the 'logic' needed to determine how to set that parameter. but, if the OP was asking about how you set an option as selected, then he posted in the wrong forum (which I don't believe he did)
-
Still have no idea what you mean. What "page" a user points to in order to perform the validation has no bearing on the actual validation - or the topic being discussed. And his query seemed perfectly self explanatory to me.
-
He didn't mean that the act of posting a form to itself is validation. But, he is posting the form back to itself because that is where he put the validation logic. This is a common practice so that when validation fails you can easily redisplay the form and, like in this case, make the form values "sticky" so the user's previous input will populate the fields. This is so the user doesn't have to enter all of the data back into a form because one field was invalid. @mattichu I typically create a function to create my select lists that takes an array of value/labels and an optional selected value. Here is an example working script <?php function createSelect($options, $selectedValue = false) { $optionsHTML = ''; foreach($options as $value => $label) { if($selectedValue!==false) { $selected = ($selectedValue == $value) ? ' selected="selected"' : ''; } $optionsHTML .= "<option value='{$value}'{$selected}>{$label}</option>\n"; } return $optionsHTML; } //Options array. This can be built from a DB query $colorOptions = array( 0 => 'Red', 1 => 'Blue', 2 => 'Green', 3 => 'Yellow', 4 => 'Brown' ); $selectedColor = isset($_POST['color']) ? $_POST['color'] : false; $colorOptions = createSelect($colorOptions, $selectedColor); ?> <html> <body> <?php print_r($_POST); ?> <form action="" method="post"> Select a color: <select name="color"> <option>-- Select One --</option> <?php echo $colorOptions; ?> </select> <button type="submit">Submit</button> </form> </body> </html>
-
I just noticed I forgot to include the $taskIDsSQL variable in the query in the processing script. And, in retrospect I would chang ethe name of that variable to $checkedTaskIDsSQL to be more descriptive of what it actually contains. Here is the updated code for the relevant section: //Process the tasks submitted $taskIDsAry = array(); if(isset($_POST['tasks')) { //Force to ints and remove NULL values $checkedTaskIDsAry = array_filter(array_map('intval', $_POST['tasks'])); } //Put in comma separated list $checkedTaskIDsSQL = implode(', ', $taskIDsAry); //Run ONE query to update the 'done' field for all the tasks //Checked tasks (in POST data) will be set to 1 //Unchecked tasks (not in POST data) will be set to 0 $query = "UPDATE tasks SET done = IF(task_id IN ({$checkedTaskIDsSQL}), 1, $taskIDsSQL) WHERE m.message_id = '{$messageID}'";
-
The OP's request has nothing to do about an AJAX solution. His request was about how to handle the dynamic number of tasks. AJAX does not, in and of itself, solve the question. It does work around some of the problems since the user would not be submitting the entire form. But, it adds complexity that is not needed and doesn't really help the OP in gaining better understanding of the basic logic that could be implemented. As for HTML standards, I use single quotes a lot due to the ease of implementing them in double quotes echo statements (but I do prefer double quotes). But, I would highly suggest the OP stop using the FONT tags which have been deprecated for over a decade now. @lewishowe, Your post only had a single query to get ALL of the undone tasks in a table. I must assume that is only mock code since I would expect that you would only be getting the tasks for a specific email/message. And, aside from the two queries (which are identical) you didn't provide any details about your table structure. So, I'll provide an example of what your tables could look like and how you could implement what you need. I expect you would need at least two tables for the email and tasks. There would obviously need to be others such as a users table, but those aren't needed as part of the functionality in question. The two tables and some of the fields needed would be something like: messages: message_id, message_date, user_id, message_text tasks: task_id, message_id (foreign key to messages table), task_description, done Now, when querying the details for a message, if you need data from the messages table and the tasks table, you will want to JOIN the tasks table on the messages table. Then, when creating the form with the checkboxes for the tasks, use the value of done (0 or 1) to make the checkboxes checked or unchecked. Also, more importantly, create the checkbox names as an array and set the value of the checkboxes as the IDs of the tasks. When a form is submitted only checkboxes that are checked are included in the POST data. So, when the user submits the form, you can update all the tasks associated with the message with a single query based upon which ones were included in the POST data. Note: none of the code below is tested, so there could be some typos. Sample code for form creation script <?php if(!isset($_GET['message_id'])) { $output .= "No message selected"; } else { //User has selected to display a message and the tasks $messageID = intval($_GET['message_id']); //Get selected message and tasks for selected message ID $query = "SELECT m.message_id, m.message_text, m.message_date, t.task_id, t.task_description, t.done FROM message AS m LEFT JOIN tasks AS t ON t.message_id = m.message_id WHERE m.message_id = '{$messageID}'"; $result = mysql_query($query); if(!$result) { $output = "Error running query:<br>Query: {$query}<br>Error:" . mysql_error(); exit(); } elseif(!mysql_num_rows($result)) { $output = "The selected message ID does not exist."; } else { while($row = mysql_fetch_assoc($result)) { if(!isset($output)) { $displayDate = date('m-d-Y', strtotime($row['date'])); $output = "<form action=\"\" method=\"post\">\n"; $output .= "<b>Date:</b> {$displayDate}<br>\n"; $output .= "<b>Message:</b> {$row['message_text']}<br><br>\n"; $output .= "<b>Tasks:</b><br>\n"; } $checked = ($row['done']==1) ? ' checked="checked"' : ''; $output .= "<input type=\"checkbox\" name=\"tasks[]\" value=\"{$row['task_id']}\"{$checked} />\n"; $output .= "{$row['task_description']}<br>\n"; } $output .= "</form>\n"; } } ?> <html> <body> <?php echo $output; ?> </body> </html> Sample code for form processing <?php if(!isset($_POST['message_id'])) { $output .= "No form submitted"; } else { //User submitted form $messageID = intval($_POST['message_id']); //Process the tasks submitted $taskIDsAry = array(); if(isset($_POST['tasks')) { //Force to ints and remove NULL values $taskIDsAry = array_filter(array_map('intval', $_POST['tasks'])); } //Put in comma separated list $taskIDsSQL = implode(', ', $taskIDsAry); //Run ONE query to update the 'done' field for all the tasks //Checked tasks (in POST data) will be set to 1 //Unchecked tasks (not in POST data) will be set to 0 $query = "UPDATE tasks SET done = IF(task_id IN (), 1, $taskIDsSQL) WHERE m.message_id = '{$messageID}'"; $result = mysql_query($query); } ?>