Psycho
Moderators-
Posts
12,157 -
Joined
-
Last visited
-
Days Won
129
Everything posted by Psycho
-
Well, there isn't going to be a "quicker" method, but depending on your needs you can create a function to return a single array with all the child values: function allSubcategoryValues($multiArray) { $outputArray = array(); foreach ($multiArray as $subCat) { $outputArray = array_merge($outputArray, $subCat); } return $outputArray; } foreach(allSubcategoryValues($subcats) as $value) { // processing here }
-
That is perfectly reasonable, although I would give the variables more logical names. dabaR's method would save a single step (save a nanosecond or two) but would give the code a more compact/logical display. However, it would not work if there are multiple main categories.
-
You answered your own question. When using the same browser, if it works outside the proxy connection and is is broken inside the proxy, the problem has something to do with the proxy connection. What that would be I have no clue.
-
I don't mind helping, but what you just asked seems logical, right? Did you even try it?
-
Select all but one (in my case two) fields in mysql database
Psycho replied to TOA's topic in PHP Coding Help
http://en.wikipedia.org/wiki/Internet_censorship_in_the_People's_Republic_of_China http://en.wikipedia.org/wiki/Censorship_in_Iran http://www.nytimes.com/2006/10/22/technology/22iht-won.3251122.html http://www.cpj.org/reports/2006/05/10-most-censored-countries.php People who have made derogatory comments about their country or leadership on the internet have been known to disappear. Don't assume everyone has the same rights as in the US -
Seriously? As I stated it is an easier implementation than I alreay handed you. The code on the actual page would not change. All you need to do is change the code on the php page that gets the image data to detemine what image to show based upon the current time. how you do that is up to you. But, you will need to determine how you will decide what image to display at any point in time. Since you are wanting a different image for each 30 second block, that means you need to define images for 2,880 different periods. You could do a massive array definining images 0 through 2,879 or you could use a database. But, assuming you don't actually have 2,880 images I would guess you have a much smaller number of images and want to rotate through them each day. When the end of the images is reached then they should start over. Here is the revised code: <?php $images = array( "aaaa.jpg", "bbbb.jpg", "cccc.jpg" ); $secondsSinceMidnight = time() - mktime(0, 0, 0, date('m'), date('d'), date('Y')); $imgIndex = ($secondsSinceMidnight/30) % count($images); header("Location: {$image}"); ?> For the 1st 30 seconds of the day image 'aaaa.jpg' will be displayed, then 'bbbb.jpg' for the 2nd 30 second block, then 'cccc.jpg' for the 3rd 30 second block. It then starts over with image 'aaaa.jpg' for the 4th 30 second block and so on. Just add all your images to the array and it should work.
-
Select all but one (in my case two) fields in mysql database
Psycho replied to TOA's topic in PHP Coding Help
Tell that to the millions of people that are persecuted every day for their beliefs. -
The question mark makes the match "non greedy". If you leave the '?' off the expression and there are two words within braces you would get everything from the start of the first word to the end of the last word (as dabaR stated). The ? modifier corrects this by making the match non greedy Regular Expression Tutorial Part 5: Greedy and Non-Greedy Quantification Example: $string = "This {is a} string {of} text."; //Without the non-greedy modifier preg_match_all('/{(.*)}/', $input, $matches); print_r($matches); //Ouput: //Array //( // [0] => Array // ( // [0] => {is a} string {of} // ) // [1] => Array // ( // [0] => is a} string {of // ) //) //With the non greedy modifier preg_match_all('/{(.*?)}/', $input, $matches); print_r($matches); //Output: //Array //( // [0] => Array // ( // [0] => {is a} // [1] => {of} // ) // [1] => Array // ( // [0] => is a // [1] => of // ) //)
-
Select all but one (in my case two) fields in mysql database
Psycho replied to TOA's topic in PHP Coding Help
Right, but just pulling all fields and displaying them can be dangerous. What happens if a new field is added that should not be displayed? That field WOULD be displayed until someone fixes that code. Imagine if a field is added to store the user's social security number, date of birth, CC number, etc. You may be the only person working on that code at the moment, but that may not always be the case. If you are working with many people or if someone takes over for you they may not know about that particular code. For security reasons it is better to explicitly state the fields to display rather than to show all (except two) by default. Using my method, if a new field is added, the developer can identify that the field needs to be added to that display or not. And, if it is missed, it is a minor bug that would be identified rather than potentially exposing sensitive data. And, yes, that is my opinion and you are free to do it as you are. No, it is not. This is the internet. People from all over the world visit this forum -
Another option '/{(.*?)}/'
-
I don't use JQuery, so I can't help you on that one.
-
Let's see the code you implemented. I see no reason it would do as you say.
-
EDIT: You already posted that you fixed the first issue, but I'm posting this anyway since it addresses some other issues in the code: Show the rendered HTML - not the server-side template code. However, your function seems a little illogical. 1. I don't see a need to pass 'true' to the function. 2. On the last line you are submitting the button? You submit the "form" not the button. but, in this case you don't need to do a submit in the javascript. The script should just return true/or false and the onsubmit trigger will behave accorddngly. Change submit trigger to onsubmit="return validateGrabber(this);" Change function to this function validateGrabber(formObj) { //Trim leading/trailing spaces var keyword = formObj.elements['keywords'].value.replace(/^\s+|\s+$/g,''); if(keyword == '' || keyword == 'search') { alert('You did not enter a search term. Please try again.'); return false; } return true; }
-
Select all but one (in my case two) fields in mysql database
Psycho replied to TOA's topic in PHP Coding Help
Uh, no. Just include the fields you DO want in your query and leave the fields you don't want out of it. Personally, I try to make my code as flexible as possible, but for something like this I think you should explicitly identify the values you want to display: <?php $query = "SELECT field1, field2, field3, field4 FROM User WHERE username='$uName'"; $result = mysql_query($query, $connection); $records = ''; while ($row = mysql_fetch_assoc($res)) { $records .= "</tr>\n"; $records .= "<td>{$row['field1']}</td>\n"; $records .= "<td>{$row['field2']}</td>\n"; $records .= "<td>{$row['field3']}</td>\n"; $records .= "<td>{$row['field4']}</td>\n"; $records .= "</tr>\n"; } ?> <table> <tr> <th>Col Header 1</th> <th>Col Header 1</th> <th>Col Header 1</th> <th>Col Header 1</th> </tr> <?php echo $records; ?> </table> -
OK, after a second review it looks like you are only using regex on the email. But, you are also doing additional string validations for specific characters. You just need to do a regex validation. Here is my email format validation function. function is_email($email) { $formatTest = '/^[\w!#$%&\'*+\-\/=?^`{|}~]+(\.[\w!#$%&\'*+\-\/=?^`{|}~]+)*@[a-z\d]([a-z\d-]{0,62}[a-z\d])?(\.[a-z\d]([a-z\d-]{0,62}[a-z\d])?)*\.[a-z]{2,6}$/i'; $lengthTest = '/^(.{1,64})@(.{4,255})$/'; return (preg_match($formatTest, $email) && preg_match($lengthTest, $email)); } Just include it in your page and call it from your current validation script $email = (isset($_POST['email'])) trim(? $_POST['email']) : ''; if(empty($email)) { $error[]=urlencode('Please enter your email.'); } elseif (!is_email($email)) { $error[] = urlencode('The Email address is invalid.'); } here is a complete explanation of the validation done //===================================================== // Function: is_email ( string $email ) // // Description: Finds whether the given string variable // is a properly formatted email. // // Parameters: $email the string being evaluated // // Return Values: Returns TRUE if $email is valid email // format, FALSE otherwise. //===================================================== // Format test // - Username: // - Can contain the following characters: // - Uppercase and lowercase English letters (a-z, A-Z) // - Digits 0 to 9 // - Characters _ ! # $ % & ' * + - / = ? ^ ` { | } ~ // - May contain '.' (periods), but cannot begin or end with a period // and they may not appear in succession (i.e. 2 or more in a row) // - Must be between 1 and 64 characters // - Domain: // - Can contain the following characters: 'a-z', 'A-Z', '0-9', '-' (hyphen), and '.' (period). // - There may be subdomains, separated by a period (.), but the combined domain may not // begin with a period and they not appear in succession (i.e. 2 or more in a row) // - Domain/Subdomain name parts may not begin or end with a hyphen // - Domain/Subdomain name parts must be between 1-64 characters // - TLD accepts: 'a-z' & 'A-Z' // // Note: the domain and tld parts must be between 4 and 256 characters total // // Length test // - Username: 1 to 64 characters // - Domain: 4 to 255 character
-
I really don't want to go through all that code trying to find out what errors you are having. I'd suggest you remove ALL the validation then add each validation feature one at a time. Test each validation feature and if it works add the next one.If a validation feature doesn't work then post the relevant code and explain what is/is not happenign and what you are wanting to happen.
-
Here are some example scripts to give you an idea: contact.php (main script called by user) <?php if(isset($_POST['name'])) { //Form was submitted, pre-process data $name = trim($_POST['name']); $email = trim($_POST['email']); $phone = trim($_POST['phone']); $message = trim($_POST['message']); //include validation script include('validate.php'); if($valid) { //Submission was valid include processing script include('process.php'); exit(); } //Validation failed, form will be displayed } //No post data or validation failed, include form include('form.php'); ?> form.php <html> <body> <div style="color:#ff0000;"><?php echo $error_message; ?></div> Contact us:<br /> <form name="contact" action="" method="POST"> <b><label for="message">Name: </label></b> <input type="text" name="name" id="name" value="<?php echo $name; ?>" /><br /> <b><label for="message">Email: </label></b> <input type="text" name="email" id="email" value="<?php echo $email; ?>" /><br /> <label for="message">Phone: </label> <input type="text" name="phone" id="name" value="<?php echo $phone; ?>" /><br /> <b><label for="message">Message</label></b> <textarea name="message"><?php echo $message; ?></textarea> <br /><br /> <button type="submit">Submit</button> </form> </body> </html> validate.php <?php $errors = array(); //Validate the data if(empty($name)) { $errors[] = " - Name is required."; } if(empty($email)) { $errors[] = " - Email is required."; } if(empty($message)) { $errors[] = " - Message is required."; } if(count($errors)==0) { $valid = true; } else { $error_message = "<b>The following errors occured:</b><br>\n"; $error_message .= implode("<br />\n", $errors); $valid = false; } ?>
-
The script which creates the form should also include the validation logic and the form should post to itself. Then if validation fails you can repopulate the form with the POSTed values. If validation passes you can then include the script wich processes the data. Of course you can separate any of the above logic into individual files. If you have a large form then you will probably want to have separate files for the form and validation as well. Here is an example of a possible file structure: contact.php (This page determines if the form data has been posted. If not, the form script is included. If post data has been submitetd (and fails) then the form script is included (and previous values populated). If validation passes, then the processing page is included) validate.php this would contain the validation logic form.php this would contain just the form with variables to populate previous data if available process.php This would be called when validation passes an would process the actual data thankyou.php This is the page to display after processing has been complete. If you call the page by using a header() to redirect, then the POST data will be cleared. This prevents data from beng submitted twice if the user hits the refresh button
-
$query = "SELECT cname, scname FROM Category c JOIN Subcategory sc ON c.cid = sc.categoryid"; $result = mysql_query($query); $current_category = ''; while($record = mysql_fetch_assoc($result)) { //Display category header (if new) if($current_category != $record['cname']) { $current_category = $record['cname']; echo "<b>{$current_category}</b><br />\n"; } //Display subcategory echo "--{$record['scname']}<br />\n"; } You should really consider making the field names consistent. For example, in the Category table you use "cid" but in the Subcategory table you use "categoryid" for the foreign key value. If they were the same, then it is apparent that they are related. Also, it allows you to do a JOIN using the USING keyword. For example, if you used cat_id in both tables the query could look like this: SELECT cname, scname FROM Category c JOIN Subcategory sc USING (cat_id)
-
Need help displaying Image and text at the same time
Psycho replied to ebolt007's topic in Javascript Help
Well there may have been some typos. Since I don't have your database or images I just wrote it on-the-fly. But, the logic is sound. Below is an HTML static version of the above which works perfectly. The only things the PHP code was doing was programatically creating 1) the javascript arrays, 2) setting the first image and description, and 3) creating the divs for the thumbs. So, take the working example below and add PHP for each of the above. Do one thing at a time and ensure the output the PHP produces is in the same format as the working example: <html> <head> <script type="text/javascript"> var image_files = new Array(); image_files[0] = 'imageA.jpg'; image_files[1] = 'imageB.jpg'; image_files[2] = 'imageC.jpg'; var image_text = new Array(); image_text[0] = 'Description for imageA.jpg'; image_text[1] = 'Description for imageB.jpg'; image_text[2] = 'Description for imageC.jpg'; function changeImage(imgIndex) { var imgObj = document.getElementById('im'); var txtObj = document.getElementById('changeText'); imgObj.src = 'images/vendors/' + image_files[imgIndex]; txtObj.innerHTML = image_text[imgIndex]; return; } </script> </head> <body> <h3>Business</h3> <br> <div class="vendor_pic_main"> <img src="images/vendors/imageA.jpg" id="im" ALT="Business" width="300"><br /> <div class="vendor_pic_main_tag" id="changeText">Description for imageA.jpg</div> </div> <div class=vendor_pic_small_holder> <div class="vendor_pic_small"> <a href="#" onclick="changeImage(0);"> <img src="images/vendors/imageA.jpg" width="40" height="40" alt="" /> </a> </div> <div class="vendor_pic_small"> <a href="#" onclick="changeImage(1);"> <img src="images/vendors/imageB.jpg" width="40" height="40" alt="" /> </a> </div> <div class="vendor_pic_small"> <a href="#" onclick="changeImage(2);"> <img src="images/vendors/imageC.jpg" width="40" height="40" alt="" /> </a> </div> </div> </body> </html> -
use the UNION operator. See tutorial here: http://www.tutorialspoint.com/mysql/mysql-union-keyword.htm
-
Need help displaying Image and text at the same time
Psycho replied to ebolt007's topic in Javascript Help
Interesting, this looks like the exact same layout that someone else was asking about the other day. Is this, by any chance, a homework problem? You are going to have to create a function to do the replacement and will need to change your onclick events to call that function. (you should of used a function in the first place instead of doing the logic in-line). Is there some reason you build all the code within echo statements? There's no reason to do that unless you are populating dynamic content into the links. So, assuming you are pulling the information from a db, here is an example: Example: <?php $query = "SELECT path, description FROM table"; $result = mysql_query($query); //Create vars for javascript arrays and thumbs $jsImageArray = ''; $jsTextArray = ''; $htmlThumbs = ''; $index = 0; while($row = mysql_fetch_assoc($result))) { $image = $row['path']; $text = $row['description']; if(!isset($first_image)) { $first_image = $image; $first_text = $text; } $jsImageArray = "image_files[{$index}] = '{$image}';\n"; $jsTextArray = "image_text[{$index}] = '{$text}';\n"; $htmlThumbs = "<div class=\"vendor_pic_small\"><a href=\"#\" onclick=\"changeImage({$index});\">"; $htmlThumbs = "<img src=\"images/vendors/{$image}\" width=\"40\" height=\"40\" alt=\"\" />"; $htmlThumbs = "</a></div>\n"; $index++; } ?> <html> <head> <script type="text/javascript"> var image_files = new Array(); <?php echo $jsImageArray; ?> var image_text = new Array(); <?php echo $jsTextArray; ?> function changeImage(imgIndex) { var imgObj = document.getElementById('im'); var txtObj = document.getElementById('changeText'); imgObj.src = 'images/vendors/' + image_files[imgIndex]; txtObj.innerHTML = image_text[imgIndex]; return; } </script> </head> <body> <h3><?php echo $Business; ?></h3> <br> <div class="vendor_pic_main"> <img src="images/vendors/<?php echo $first_image; ?>" id="im" ALT="<?php echo $Business; ?>" width="300"><br /> <div class="vendor_pic_main_tag" id='changeText'><?php echo $first_text; ?></div> </div> <div class=vendor_pic_small_holder> '.\n; <?php echo $htmlThumbs; ?> </div> </body> </html> -
You would only need to do such a thing if you are using a collation for your database field which is case sensitive. Since you are trying to write code to work around the case of the input I'm guessing you are using the wrong collation type and should be using a case insensitive collation. Try changin the collation type pf that field to something like "latin1_general_ci". I'm pretty sure the "i" on the end indicates an insensitive collation. http://dev.mysql.com/doc/refman/5.0/en/charset-collation-implementations.html
-
Just a tip. If you put all your logic (i.e. PHP) at the top of the page and the presentation (HTML) at the bottom you can avoid these types of errors by giving the script a more logic structure. Also, this line seems to serve no purpose $totalRows_rsSubjects = mysql_num_rows($rsSubjects); Here is how I would contrcut that page: <?php mysql_select_db($database_WidgetCorp, $WidgetCorp); $query_rsSubjects = "SELECT * FROM subjects"; $rsSubjects = mysql_query($query_rsSubjects, $WidgetCorp) or die(mysql_error()); $row_rsSubjects = mysql_fetch_assoc($rsSubjects); $menuHTML = ''; while($row = mysql_fetch_assoc($rsSubjects)) { $menuHTML = "{$row['menu_name']} {$row['position']}\n"; } ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Test1</title> <link href="public2.css" rel="stylesheet" type="text/css" /> <style type="text/css"> <!-- body { background-color: #FFC; list-style: circle inside; } a { font-size: medium; list-style: circle outside; } a:link { color: #900; } --> </style> </head> <body> <div id="header"> <h1>Header Widget Corp</h1></div> <div id="Main_Body"> <h1 class="h1MainBody">Staff Menu</h1> <p class="pMainBody">Welcome to our Staff area</p> <p class="pMainBody"><a href="content.php" title="Manage Website content">Manage Website Content</a></p> <p class="pMainBody"><a href="new_user.php" title="new_user">Add Staff User</a></p> <p class="pMainBody"><a href="Logout.php" title="logout">Logout</a></p> </div> <div id="left"> <p class = "navbar"> <?php echo $menuHTML; ?> </p> <!-- "<br/>" --> </div>
-
I suspect the problem is one of two things: 1. You did not post ALL of the code between the line on which the query is run and the WHILE loop. Pehaps there is a mysql_fetch_assoc() run before the loop? I've seen it many times. 2. The values in the first record of data contains characters that are being interpreted as HTML code. Post a little more of the code as I suspect the error is in the code and not the value.