Psycho
Moderators-
Posts
12,157 -
Joined
-
Last visited
-
Days Won
129
Everything posted by Psycho
-
Let me rephrase your question another way. I think you want a list of all users who meet one of two criteria: 1. They have a child registered for the current season OR 2. They are listed in the leaders table. So why not create a simple query against the users table using a clause in your WHERE statement for matching users IDs for those two conditions: SELECT users.userID, users.fName, users.lName FROM users WHERE users.userID IN (SELECT parents.parentID FROM parents JOIN registrations ON parents.childID = registrations.userID WHERE registrations.seasonID =:seasonID UNION SELECT userID FROM leaders) AND users.fName != '' && users.lName like :lName Not tested, but it should be close
-
You don't connect your lights to the router. There has to be separate hardware to actually run home automation. Think of it like a home security system. You don't connect the door trigger to your phone line and have it contact the security company when there is a problem. There will be a central device that you connect all of the door/window/fare alarm/etc triggers to. Then you can program that device as to what each trigger is and any rules for when things should be armed and such. Then, whenever a device is triggered it is picked up by the central device that then contacts the security company (over your phone line) and reports the problem. In the same way, for home automation you need a central device that is connected to all of the things you want to automate. In addition, you typically need something connected to each things you want to automate. So, for a light, you would have a small device that plugs into the outlet and the light plugs into that device. That device is connected to the "central device" via the network or a proprietary communication line. That central device will be the "brains" of the home automation and run all the connected equipment. It is THIS device that you will need to communicate with. So, without such a device in hand, there is no way to tell you how to proceed. You will need to get such a device and find out how you can communicate with it. You might be able to communicate with it through the network or you may have to connect it to a PC via serial port. Here is an article entitle "An Introduction To Home Automation" which discusses hardware, protocols, web-based interfaces, etc.. I suggest you read it or go find some additional information on how home automation works. http://www.tomshardware.com/reviews/home-automation-insteon,2308.html Did you pick this final project without having a clue on how home automation works?
-
Agreed. You also need to know 1) What data you can retrieve from the home automation (HA) system and 2) what commands you can send From there you can determine what kind of interface and features to create. Let's analyze a "possible" example: , suppose the HA system has a list of "zones" to control lighting. You should, hopefully, be able to query the HA system to get a list of those zones and the current lighting status (on/off). But, let's assume you only get names such as zone1, zone2, zone3, etc.. If that was the case, I would build at least a couple features: 1) I would build a management feature that allows the user to see the current list of zones reported from the HA system and assign a "friendly" names to them. 2) I would also have a status page that lists all the zones with their "friendly" names (if entered) along with the current lighting status. That page would probably include a link for each zone to add/modify the friendly name. It would also include a button to change the status of the lights from off to on or vice versa.
-
No need to use PHP to get the random value. That can be done with MySQL in the first query <?php require('members/connect.php'); // Get a count of the banners available $query = "SELECT FLOOR(COUNT(*) * RAND()) AS RandOffset FROM banners WHERE banner_size=7"; $result = mysqli_query($dbc, $query); $banner = mysqli_fetch_array($result); mysqli_free_result($result); // Select the banner $query = "SELECT http_url, onMouseDown_site, title, banner_url FROM banners WHERE banner_size = 7 LIMIT {$banner['RandOffset']}, 1"; $result = mysqli_query($dbc, $query); $banner = mysqli_fetch_array($result); mysqli_free_result($result); ?> <a href="http://<?php echo $banner[0]; ?>">. . . My banner stuff here . . .</a>
-
how to run javascript even browser is closed
Psycho replied to phpdeveloper123's topic in Javascript Help
What are you trying to achieve exactly? -
OK, I was in a good mood. A quick google search directed me to a tutorial on this very site (http://www.phpfreaks.com/tutorial/php-add-text-to-image) which I used to create the following: <?php error_reporting(E_ALL); //Set values based upon form POST data $fullname = "Ricardo Montobaln"; $location = "Denver, Colorado"; $school = "School of Hard Knocks"; $facebook = "www.facebook.com/myprofile"; $email = "myname@domain.com"; //Path to image $imageName = 'images/medium.png'; //Font face and size $font = 'verdana.ttf'; $fontSize = 10; $spacing = 22; //horizontal spacing between lines //Create image object $image = ImageCreateFromPng($imageName ); //Pick color for the text $fontColor = imagecolorallocate($image, 255, 255, 255); //Set start x & y positions $x = 75; $y = 16; //Add name text imagettftext($image, $fontSize, 0, $x, $y, $fontColor, $font, $fullname); //Move y position down and add location text imagettftext($image, $fontSize, 0, $x, $y+=$spacing, $fontColor, $font, $location); //Move y position down and add school text imagettftext($image, $fontSize, 0, $x, $y+=$spacing, $fontColor, $font, $school); //Move y position down and add facebook text imagettftext($image, $fontSize, 0, $x, $y+=$spacing, $fontColor, $font, $facebook); //Move y position down and add email text imagettftext($image, $fontSize, 0, $x, $y+=$spacing, $fontColor, $font, $email); header('Content-Type: image/png'); //Output image to the browser imagepng($image); //Delete the image resource imagedestroy($image); ?> Note: You set the position of the first line using the values for $x and $y. Then the $spacing variable is used to move down to the next line after you place a line of text. That is done by using "$y+=$spacing" inside the function call for imagettftext(). That will increment $y by the value of $spacing. EDIT: you may want to think abut reducing the font size. Otherwise values that are a little long will go past the right edge of the black area. Or you could make the image wider.
-
This forum (PHP Coding Help) is for people to get help with code they have written. There is a separate forum here for Freelancing if you want someone to write the code for you.
-
Do you understand what is meant by pseudo code? It is just mock code to illustrate the logic to be used - not actual working code. while(next ($row)){ means nothing in your actual code above. It was just meant to illustrate getting the next record. if($execute) { $con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); $query = "SELECT book.id, book.title, book.url, book.viewer, book.sub_id, subj.subject_id, subj.name FROM book LEFT JOIN subj ON(book.sub_id = subj.subject_id) WHERE book.username_id = :yourid ORDER BY subj.subject_id"; $sthandre = $con->prepare($query); $sthandre->execute(array(':yourid'=>$userid)); $prevSubject = false; while ($row = $sthandre->fetch()) { $bmid = ($row['id']); $title = ($row['title']); $url = ($row['url']); $audience = ucfirst($row ['viewer']); $subid = ($row['subject_id']); $subjectname = ($row['name']); if($prevSubject == $subjectname) { //If subject name same as last, then empty value // - or do whatever you want to do differently $subjectname = ''; } //somehow if the subject name is a duplicate echo it once and carry on with the other variables looping echo "$subjectname, $title, $audience" $prevSubject = $subjectname } }
- 6 replies
-
- loop
- duplicates
-
(and 1 more)
Tagged with:
-
Where is your code? Barand only provided pseudo code, so we need to see what you implemented.
- 6 replies
-
- loop
- duplicates
-
(and 1 more)
Tagged with:
-
The page you linked to used JavaScript.
-
Here is the solution that relies on the query selecting the random image <?php $query = "SELECT * FROM ( SELECT category.category_id, category.category_name, photos.photo_filename FROM gallery_category AS category LEFT JOIN gallery_photos AS photos ON category.category_id = photos.photo_category WHERE category.photo_owner = '1' ORDER BY RAND() ) AS T GROUP BY category_id ORDER BY category_name"; $result = $mysqli->query($query) or die($mysqli->error.__LINE__); //Define number of columns to use $maxColumns = 5; $count = 0; //Var to count records $categoryList = ''; //Var to hold HTML output while($row = $result->fetch_assoc()) { $count++; //Increment count $imgSrc = (!empty($row['photo_filename'])) ? "clients/{$client}/photos/{$row['photo_filename']}" : 'images/noimage.jpg'; //Open new row if first record in row if($count % $maxColumns == 1) { $categoryList .= "<tr>\n"; } //Output current record $categoryList .= "<td width='20%' style='text-align:center;padding-top:10px;padding-bottom:10px;'>"; $categoryList .= "<img src='{$imgSrc}' height='100' width='100'><br>"; $categoryList .= "<a class=black-text href='showphoto.php?cid={$row['category_id']}'>{$row['category_name']}</a>"; $categoryList .= "</td>\n"; //Close row if last record in row if($count % $maxColumns == 0) { $categoryList .= "</tr>\n"; } } //Close last row if needed if($maxColumns % $count != 0) { $categoryList .= "</tr>\n"; } ?> <table width="95%" border="0"> <?php echo $categoryList; ?> </table> Here is the solution that relies on the PHP code <?php $query = "SELECT category.category_id, category.category_name, GROUP_CONCAT(photos.photo_filename) as photos FROM gallery_category AS category LEFT JOIN gallery_photos AS photos ON category.category_id = photos.photo_category WHERE category.photo_owner = '$client' GROUP BY category.category_id ORDER BY category.category_name"; $result = $mysqli->query($query) or die($mysqli->error.__LINE__); //Define number of columns to use $maxColumns = 5; $count = 0; //Var to count records $categoryList = ''; //Var to hold HTML output while($row = $result->fetch_assoc()) { $count++; //Increment count if(empty($row['photo_filename'])) { //Use default image $imgSrc = 'images/noimage.jpg'; } else { //Set Random image $imagesAry = explode($row['photos']); $imgName = $imagesAry[array_rand($imagesAry)]; $imgSrc = "clients/{$client}/photos/{$imgName}"; } //Open new row if first record in row if($count % $maxColumns == 1) { $categoryList .= "<tr>\n"; } //Output current record $categoryList .= "<td width='20%' style='text-align:center;padding-top:10px;padding-bottom:10px;'>"; $categoryList .= "<img src='{$imgSrc}' height='100' width='100'><br>"; $categoryList .= "<a class=black-text href='showphoto.php?cid={$row['category_id']}'>{$row['category_name']}</a>"; $categoryList .= "</td>\n"; //Close row if last record in row if($count % $maxColumns == 0) { $categoryList .= "</tr>\n"; } } //Close last row if needed if($maxColumns % $count != 0) { $categoryList .= "</tr>\n"; } ?> <table width="95%" border="0"> <?php echo $categoryList; ?> </table> Note: I removed the center tag and the beginning and ending line breaks inside the TD's and instead added style properties to the TD. But, really, you should assign a class to the TDs and define the class. Better yet, you can assign a class to the table and then just define the class to apply to the TD elements of the table. Makes for much cleaner code.
-
http://forums.phpfreaks.com/topic/247904-select-a-random-row-with-group-by/ EDIT: I had actually written out the query (from the above post) to match your specific table structures along with some other comments. So, you can use the query format in that post above, but it uses ORDER BY RAND() in order to get the random image on each run. That can be a performance issue with large data sets. This may not be an issue with your current setup. I would probably try it out and see what the performance is like since that is the easiest solution.. And, I'd probably load up the database with a bunch of test data to simulate what you think you may have. If the performance is acceptable, then use it. The other option would be to query the database so you get one record back for each category that includes a concatenated list of all the photos for the category. Then use PHP array functions to get a unique value.
-
Well, yes. But, in the code you have above it is querying all the associated records displaying the specific image to display for each record in the result set. So, if you want the image to be random you must have a list of possible values to select the random value from. But,since you are specifying a specific image, not sure what you are looking to do.
-
Um yeah, I think the reason Jessica got frustrated is that you are not really reading the information provided and are instead filtering it through some preconceived ideas. I specifically stated "You don't need any of that logic" and said you could do the whole thing with your query. So, what do you do? You implement that query using the logic you already have and get unexpected results. I even provided an example of what the database result set would be - I thought that would be enough for you to understand how you could implement it. The least you could have done was to run the query through PHPMyAdmin, or whatever you are using to manage your database, and see that it works before even trying to implement it in your code. Those of us who routinely respond to posts enjoy helping people. We don't get paid to do this. So, when someone receiving our help does not time the time and consideration to really read what is provided or complains about a simple typo in code that we provide it seems the person is not appreciative of our time and effort. So, let me spell it out for you. This should be all you need: include("./sql-conn.php"); $db = mysql_connect($hostname, $username, $password) or die('Failed to connect to database: ' . mysql_error()); mysql_select_db($database); $query = "SELECT IF(`hide`=1, 'none', `class_1`) AS `option`, GROUP_CONCAT(`name` SEPARATOR ',') AS `names` FROM class GROUP BY `option` ORDER BY `option`='none', `option`, `name` AS"; $result = mysql_query($query) or die('Failed Getting Results: ' . mysql_error()); while ($row = mysql_fetch_assoc($result)) { echo "<b>{$row['option']}</b><br>{$row['names']}<br><br>\n"; } However, that will output the names as a comma separated list (due to the GROUP_CONCAT in the query). In the output you want you apparently want a line break after each name. That is a trivial problem to solve which I expect YOU to solve. You can either implement that in the query by changing the string that is used to concatenate the names or you can take the comma separated string of names and implement the logic in the PHP code. I would do the latter.
-
PHP generate form from data, return to calling script issue
Psycho replied to Sonzai's topic in PHP Coding Help
You didn't pay attention to what I posted. I did not provide this: echo ' <td><input type="checkbox" name="Updates[]" value="{$index}"></td> '; I provided this echo " <td><input type=\"checkbox\" name=\"Updates[]\" value=\"{$index}\"></td>\n"; Which will result in the exact same result as echo ' <td><input type="checkbox" name="Updates[]" value=" '. $index .' "></td> '; Note the double quotes I used to define the string vs. the single quotes you used. That makes all the difference. If you are going to try and correct someone, be sure you are right. From the manual: http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing emphasis added -
You don't need any of that logic. You can do the whole thing with your query SELECT IF(`hide`=1, 'none', `class_1`) AS `option`, GROUP_CONCAT(`name` SEPARATOR ', ') AS `names` FROM class GROUP BY `option` ORDER BY `option`='none', `option`, `name` ASC Using the sample data you showed above the result of that query would be option | names ----------------------- Option A | John Option B | Bob Option C | Chris None | Tom, Sam
-
Preventing multiple logins using the same account IS a valid concern. It all depends on the business needs. One scenario is if you are selling the application on a per-user license. If you allow multiple people to log in using the same credentials you will lose money. But, this is not a decision to make lightly as illustrated by Jessica's example. If you really do need to prevent this, then you can implement a simple solution so when someone logs in and previous logins on that account would be invalid. In addition to creating a session when authentication occurs, store the Session ID in the user table. Then on any page requests you should already be doing a 'login" check you can query the user record to see if the stored session matches the current session - if not, log the user out. This will basically make the last login the active one.
-
PHP generate form from data, return to calling script issue
Psycho replied to Sonzai's topic in PHP Coding Help
Hard to say. If the file being called has a php extension and it is properly constructed with opening and closing PHP tags <?php ?> then it should be parsed by the PHP engine. That isn't what made it run correctly, but that is a better way to know if a form was POSTed. If you have multiple forms that are posted, then you would do another check to know which one. I would typically use a hidden form field with a set value for that. So it is. I hadn't seen that change yet. But, not sure why you needed it in this case. It's not even fully supported yet (OK, on not supported in IE apparently, but still). based upon your layout, you can include all the fields in the FORM opening and closing tags, so I see no reason to add complexity. -
As a "general" rule, if you have a value in a table that is a finite list such as the above for country, tier, and type you should typically put those values into a separate table and JOIN as needed. There are many reasons to do this aside from sorting. Here are some: Keeping the values consistent. By using the values from a lookup table you know they will always be the same (e.g. "USA" vs "Usa" vs "U.S.A"). Plus, if you ever need to change the text of a value, you can do it by changing just one record. It helps to prevent invalid values. Even when using a SELECT list in an HTML form, it is possible for other values to be submitted. Using a lookup table makes it much easier to prevent invalid values from being inserted into the database. If you do want to create a form with a select list of "valid" values you can get that list directly from the lookup table rather than doing a DISTINCT query or hard-coding the list in the code. You can relate records using the same lookup values very easily using just the ID of the value rather than the text of the value. I'm not 100% sure, but I would guess that even when indexing, that indexes on integer fields will be more efficient than indexes on text fields
-
I second Brand's suggestion to use one table for the shifts and adding a new column to identify day vs. night. The above query may get you though the current problem, but you are only creating more headaches for yourself if you don't fix it now.
-
PHP generate form from data, return to calling script issue
Psycho replied to Sonzai's topic in PHP Coding Help
Not sure what you mean to do with the data when the form is posted - so I didn't touch that part of your code, but this should get you on the right path: phpfrom.php <?php $dataAry = array(); $dataAry[] = array( 'Name' => 'Haru', 'Age' => NULL, 'Sign' => 'Pisces' ); $dataAry[] = array( 'Name' => 'Shizuku', 'Age' => 15, 'Sign' => NULL ); $dataAry[] = array( 'Name' => 'Yamaken', 'Age' => NULL, 'Sign' => NULL ); include ('phpto.php'); if($_SERVER['REQUEST_METHOD'] == 'POST') { echo " the rest is gone<br>\n"; $updates = $_POST['Updates']; $nums = count($updates); echo "number in updates[] " . $nums . "<br>\n"; if($nums >= 0) { $dataAry[$nums]['Sign'] = 'Ophiuchus '; echo $dataAry[$nums]['Name'] . " is now an Ophiuchus"; } } ?> phpto.php <html> <head></head> <body> <form method="POST" action="" name="corrections"> <table cellpadding="5" border="1" > <tr><td><h3>Name</h3></td> <td><h3>Age</h3></td> <td><h3>Sign</h3></td> <td><h3>Update</h3></td> </tr> <?php foreach($dataAry as $index => $data) { echo " <tr>\n"; echo " <tr><td>{$data['Name']}</td>\n"; if($data['Age'] == NULL) { echo " <td><input type=\"text\" name=\"Ages[{$index}]\"></td>\n"; } else { echo " <td>{$data['Age']}</td>\n" ; } if($data['Sign'] == NULL) { echo " <td><input type=\"text\" name=\"Signs[{$index}]\"></td>\n"; } else { echo " <td>{$data['Sign']}</td>\n"; } echo " <td><input type=\"checkbox\" name=\"Updates[]\" value=\"{$index}\"></td>\n"; echo " <\tr>\n"; } ?> </table> <input type="hidden" name="test"> <input type="submit" value="submit"/> </form> </body> </html> -
PHP generate form from data, return to calling script issue
Psycho replied to Sonzai's topic in PHP Coding Help
There are several problems I see there, but the biggest is this form="corrections" There is no "form" parameter for input fields. That is what is causing your form not to post. -
If you dont understand something - then ask. <?php include("includes/db.config.php"); $plc = isset($_GET['PLC']) ? trim($_GET['PLC']) : ''; $tableHTML = ''; if(!empty($plc)) { $plcSQL = mysql_escape_string($_GET['PLC']); $query = "SELECT id, PLC, DateOpened, Raised_by, Block, Description FROM log WHERE PLC LIKE '%{$plcSQL}%' ORDER BY DateOpened DESC "; $result = mysql_query($sql); if(!$result) { $tableHTML .= "<tr><td>There was an error running the query</td></tr>\n"; //Uncomment next lines for debugging only //$tableHTML .= "<tr><td>Query: {$query}</td></tr>"; //$tableHTML .= "<tr><td>Error: " . mysql_error() . "</td></tr>"; } elseif(!mysql_num_rows($result)) { $tableHTML .= "<tr><td>There were no matches for the search</td></tr>"; } else { while ($row = mysql_fetch_assoc($result)) { $tableHTML .= "<tr>\n"; $tableHTML .= " <td width=\"133\"><a href=\"ocado_edit_my_log.php?id={$row['id']}\">Edit</a></td>\n"; $tableHTML .= " <td width=\"543\"><a href=\"singlelog.php?id={$row['id']}\">{$row['PLC']}</a></td>\n"; $tableHTML .= "</tr>\n"; $tableHTML .= "<tr>\n"; $tableHTML .= " <td>Date raised</td>\n"; $tableHTML .= " <td>{$row['DateOpened']}</td>\n"; $tableHTML .= "</tr>\n"; $tableHTML .= "<tr>\n"; $tableHTML .= " <td>raised by</td>\n"; $tableHTML .= " <td>{$row['Raised_by']}</td>\n"; $tableHTML .= "</tr>\n"; $tableHTML .= "<tr>\n"; $tableHTML .= " <td>Block</td>\n"; $tableHTML .= " <td>{$row['Block']}</td>\n"; $tableHTML .= "</tr>\n"; $tableHTML .= "<tr>\n"; $tableHTML .= " <td>Description</td>\n"; $tableHTML .= " <td>{$row['Description']}</td>\n"; $tableHTML .= "</tr>\n"; } } } ?> <form> <span class="ContentStyle">Please enter PLC name </span><span class="style4"> </span> <input type="text" name="PLC" size="20" maxlength="20" value="<?=$plc?>" > <input name="submit" type="submit" value="Search"> <span></span> </form> <table width="692"> <?php echo $tableHTML; ?> </table>
-
Database text fields contain "rn" instead of blank lines
Psycho replied to LLLLLLL's topic in MySQL Help
I'm guessing that somewhere in the code somebody inserted a stripslashes() on data before it was inserted into the database. So, what would have been "\r\n" would now be "rn". And, now that I think of it, maybe that stripslashes() was already there to workaround problems with having magic quotes turned on in the server. And, maybe that option has been turned off. -
A few things: When viewing a list of products, there are five associated links: Product name, Post Date, Post User, Comments and "Read More". The Product name and "read More" links take the user to a product specific page, the Post User takes the user to a list of products added by that user and the comments takes to the user to the comments for the product. But, the Post Date link takes the user to the product page. I would think it would take me to a list of all products posted on that date. The Product Name search seems to search the description of the product and not the name. So a search for "Ducati" brings up products such as "Honda" that reference ducati products in their description. If the search is going to be on the description then the title of the search probably shouldn't be "Product name" The "Tagged with" and "Posted in" listed at the bottom of a product page have links, but they are in black text and don't look like links