Psycho
Moderators-
Posts
12,157 -
Joined
-
Last visited
-
Days Won
129
Everything posted by Psycho
-
Optimising website for mobile androds and iphones
Psycho replied to Leverkusen's topic in PHP Coding Help
Optimizing probably isn't the right word. Responsive and adaptive web design are probably what you are looking for. This involves code changes such that the output of the web page will "work" on a mobile device. This typically means a single code base but different layouts. There is a plug-in for Chrome called User Agent Switcher that allows you to trick the website into thinking your browser is a mobile device. You can then reduce the size of the browser window to make it seem as if you are on a device with a small(ish) display. This allows you to look at sited that work well in both desktop and mobile devices to see what decisions they made to implement their site for both types of output. -
Check your spelling! if(!isset($_SESSION['rmamr'])){ $rmanr = $datestrip; $rmanr .= $lastdetid; $_SESSION['rmanr'] = $rmanr; } else { $rmanr = $_SESSION['rmanr']; } The isset() check is using the index 'rmamr' whereas the index you use set is 'rmanr'. You might also consider giving that index a more descriptive value anyway.
-
Are you starting the session on each page load? E.g. session_start(). But, aside from that, one thing in your code makes no sense. Why are you creating a date string in the format YYYY-mm-dd only to then strip out the dashes? WHy not create the date in that manner to begin with?
-
Since you've provided absolutely no details of the process in question, it is impossible to say. However, if I make an assumption that the progress bar is controlled through AJAX, I assume your concern is what happens if responses from the service are no longer sent or return an error. The back end process should have error handling implemented in order to return an error if one is detected and the AJAX code should look for those and respond appropriately. However, if the PHP script just fails with some unhanded error you should get some response. Again, the AJAX code needs to try an detect that and respond accordingly. Lastly, if the back end service just doesn't respond then you can implement a timer on the JavaScript side to include a timer. If no response is returned in a specified time provide an error condition.
-
@lioslios: Do you need to consider the conversions to Daylight Savings to Standard Time? Are you OK with one day a year that the first 5 hours would be considered the first 4 hours and one day a year the first 3 hours would be considered the first four? If you had a block for 2 hours or less it would get really weird. It may not be a concern, but if this is something that is processed every day, depending on the usage, you may have to make allowances for it.
-
Yeah, I'm a bit confused as to why you would want the data in that format. Should definitely use a more logical structure. I would suggest something like this array ( [status] => 'ok', ['results'] => array ( 'test1' => '67', 'test2' => '37', 'test3' => '37' ) ) Then encode it in JSON and send to JS which will decode it into an array.
-
What does " . . . the new system doesn't know how to open these files . . . " mean? How are you trying to "open" them and for what purpose? Are you trying to open them through PHP in some manner? Or, are you trying to open them outside of a PHP application? If the problem is trying to open these outside of any PHP logic, then you could modify the upload process to use finfo_file() as suggested above and dynamically add an appropriate extension to the file name when saved. That way Windows (assuming) would know the appropriate application to use to open the file. If the problem is with some process in PHP that "opens" the file, then some more details around what you are doing with the file may be helpful
-
Calculating values returned from checkboxes using array_sum()
Psycho replied to terungwa's topic in PHP Coding Help
I thought about that too. If this is just an learning exercise or for personal use that is fine. But what people ask for is almost never what they really need. Although, the script (as it is now) is just echoing the total to the page I didn't want to assume that it was just throwaway data such as that. For all I know the score would be stored as part of a test/exercise/evaluation. It *may* be something that a person would be interested in manipulating the results through the submission data. So, I (almost) always only pass the selections and do all calculations from the live data on the back end. -
Calculating values returned from checkboxes using array_sum()
Psycho replied to terungwa's topic in PHP Coding Help
The problem is a typo. The form fields are using "lifeEvent", but the PHP code is using "lifeEvents". Change the name to be the same in both. -
Calculating values returned from checkboxes using array_sum()
Psycho replied to terungwa's topic in PHP Coding Help
Where's the code you are using to create the checkbox fields? I'm guessing they aren't created as an array -
Yeah, that block of code is hard to read. Never liked that alternative syntax. That code could be rewritten as if ( have_posts() ) { while ( have_posts() ) { the_post(); the_content(); } } else { echo "<p>Sorry, no posts matched your criteria.</p>"; }
-
I would suggest the following: Set the default style of the text as black and make its display property 'none'. So, it won't be in the initial page load at all. Then, perform the background transition to black. Once that completes, change the display property of the text to in-line and perform the transition to white
-
Calculating values returned from checkboxes using array_sum()
Psycho replied to terungwa's topic in PHP Coding Help
You are giving all of the checkboxes the same name. You can't do that. Typically in those situations only the last field will be passed. You need to give each field a unique name or make the name an array. In this case, you shoudl make the field an array and use the life event ID as the index for each field. But, there are some problems that have to be resolved. You are hard coding the stress factor for each stress event. This should be stored as part of the data for the events in the database. By, hard coding the values in the code you have created a dependency between the DB records and the code that makes this unmanageable. If you were to add an event in the future you would have to modify the code as well. That's a bad design. A couple other tips. Don't use extract(). There are lots of reasons why that I won't go into. Also, I hope you are not using SELECT * for your query. Only select the fields you need. So, here is what I would do: 1. Update the table to include the stress factor for each life event record 2. Create your checkboxes as an array with the value of each option as the life event ID while($row = mysqli_fetch_assoc($result)) { echo "<tr> <td style='width:30px'>{$row['life_event_id']}</td> <td style='width:300px'>{$row['life_event']}</td> <td><input type='checkbox' name='lifeEvent[]' value='{$row['life_event_id']}' /></td> </tr>"; } 3. In the processing code, get the selected life event IDs passed in the POST data and get the sum of the stress factor by running a query. if (isset($_POST['lifeEvents'])) { $checkedEvents = array_map('intval', $_POST['lifeEvents']); $checkedEventsCSV = implode(',', $checkedEvents); $query = "SELECT SUM(stress_factor) FROM life_events WHERE life_event_id IN ({$checkedEventsCSV})"; $result = mysql_query($query); $stressScore = mysql_result($result, 0); echo "Your Stress Score = {$stressScore}\n"; } You can now add/edit the data in the database without any changes needed to the code. -
You say you have a weird transition problem and then post some pics. So, I have an idea of what IS happening. But, I have no idea what you are wanting to happen. Are you wanting to stop after the 2nd image (i.e. not have the text transition to black)? The transition would be controlled by JS, yet you've not provided that code.
-
Capitalize and remove spaces after form submission
Psycho replied to Stefan83's topic in PHP Coding Help
I was going to post almost the exact same thing as mogosselin, but he beat me to it. Using array_map() with a separate function allows you to decouple the manipulation of the input from the processing of the input. Plus, this is a personal preference, I would put each step of the string manipulation on a separate line instead of one line. Makes it easier to "see" what is going on when you have to debug issues later. Then again, since rpost is a custom function, you could potentially update that to do the additional work and call it using array_map(). FYI: The fact that no one has a clue what rgpost() does is a good indication that it is not well named. Don't be afraid of a long name if it helps to identify it's purpose and/or what it does. -
That makes no sense. The first code snippet is checking to see if $_POST['post_id'] is set. If so, it sets that as the value of $the_team. Otherwise it sets the value to 0. That's a pretty common practice to prevent errors in case nothing is passed to the script. I don't see any typos in that line that would cause it to fail. So, I can't explain it. Go with the second option if you want. Just understand that you could get an error if no value is passed to that script.
-
You will likely need to build a script to be executed one-time to update the records after you revise the tables. It may be able to be accomplished by running a single query, but I don't think so. So, create a prepared statement to insert new records into the podcats table. Run a query to get all the records. Then, while looping through those results, get the SKU and the comma separated categories. Explode the categories on the commas and run the prepared statement for each category passing the SKU and the category value. After you've done this and verified the results you can delete the category field from the original table
-
I'm not following. Are you talking about the default white background that EVERY page starts with until the page loads and styles can be applied? At least, that is all I see when I refresh that page. If so, there's nothing you can do about that. When you click refresh, the browser starts "from scratch". Every page starts loading with a white background. Although that is a feature of the browsers not anything to do with the code to build the pages. It may be something that can be changed in the browser, but it would be a change by the individual users, not something that could be controlled in the code for the page. Well, it *may* be possible to prevent the refresh at all, but I can't see anything good coming from that. If you don't want that momentary flash of white when loading a new page (i.e. clicking a link), then you could implement AJAX to change the content dynamically without a page refresh. But, if the user explicitly clicks refresh it will start over. Yes, I do. It's right there in the source code.
-
Movie Online Trivia Game - An Original Website
Psycho replied to Strider64's topic in Website Critique
It's not clear how the point system works. Every time I answered a question correctly I think I got 100 points. But, I'm not sure if the time remaining has any impact on points. Plus, when I answered a question wrong I lost 25 points. What if I let the time expire? Do I still lose 25 points, or is it only for answering incorrectly. I think there needs to be some text/indication on the page to let the user know how the points works. If the points change based upon how fast the user answers, then it'd be nice to see the "win points" available as the clock is ticking down. When the time ends without an answer, nothing happens. No out of time message or anything. I would think that it would reveal the correct answer. After a question is answered, or time runs out, the questions should no longer be links. Suggest showing the unanswered questions in a gray background as a visual queue Is there an end? It doesn't tell me "Question 5 of 20" or anything like that. So, what's the point if there is no end? Needs some proofreading for typos and grammar issues One question for the Tom Hanks move had the answers "Castaway" and "Cast Away". Really? That's splitting hairs a bit isn't it? I found all the questions incredibly easy for movies I had seen. I only had problems with movies I had not seen. -
Well, this jumps out of me: In the AJAX function you pass the value using this type : "POST", But, in the loop.php file you retrieve the value using $the_team = (isset($_GET['post_id'])) ? $_GET['post_id'] : 0; Note: You should set a default response from loop.php when there are no results so you can detect the root cause easier.
-
You may also have a problem with the field "codes" - the name suggests multiple. So, if you are putting multiple codes in that field, those should be broken out to a separate table as well. But, assuming codes is not storing multiple values and your previous description, the tables might look like this pods ------------------- sku (primary key) codes podcats ------------------ sku (foreign key) category Some data to illustrate how they are used: pods -------------- sku | codes -------------- A 123 B 456 C 789 podcats -------------- sku | category- --------------- A Ceramics B Ceramics B Sale items So, the item with SKU A is only associate with the category Ceramics. But, the item with SKU B is associated with categories Ceramics and Sale items. You can now run a simple query to find all itesm associated with the category Ceramics, such as SELECT * FROM pods JOIN podcats on pods.sku = podcats.sku WHERE podcats.category = 'Ceramics' Although, a purist would state that there should be a third table to describe the unique categories and then only reference the category ID in the podcats table.
-
I wouldn't do that. It makes no send to show the previous result if you are going to immediately refresh it. That assumes the user knows that the data that is initially loaded is old(er) data and will be update after X seconds. I think it all depends on how fresh the data needs to be. The OP stated he is fine with cached data so I'd go that route for now. No need to over-complicate with multiple thing at once anyway. Not sure what you are getting at with the question about sessions. On a Linux box you would use a CRON job. If you are on Windows and you have full rights on that box, then just set up a windows scheduled task. Set that task to 'execute' the PHP file. The command would look something like this C:\Path\to\php.exe -f "C:\Path\to\file.php" Here are the basic steps needed to implement this: 1. Add a new column to your table for status. I would also add another field called 'last_updated'. Make that one a timestamp with the optional configuration to auto-update with the current timestamp whenever the record is modified. 2. In the script to display the server to the user, be sure to include the new status field (list out only the fields you need in the SELECT query as Zane advised) 3. Create a new script called something like 'updateStatuses.php'. Have that script do the following: 1) Query the servers to be updated, 2) While looping through the results get the current status for each server and save the status to an array, 3) After processing all of the servers, run a single UPDATE query to update all the records with the current status 4. Create a scheduled tasks to run the above script at a specified interval
-
Good catch. If that's the case then the table is named incorrectly. lectures: this is plural form for lecture (i.e. a verbal lesson) lecturers: this is the plural form for a lecturer (i.e a PERSON that performs a lecture) EDIT: I just looked at the table names again and I thought I had misread it due to it being in French or another language. But, it looks like the spelling is just wrong in multiple places - not even consistently incorrect?! lecutures: should be lecturers lecuter_medium: should be lecture_medium lecture_subject_category: This one is right As for field names: lecture_id: Used in three places. It is spelled correctly, but should really be lecturer_id lecuture_id: Used in the "lecuter_medium" table.
-
I concur with Barand. We would have to have a deep understanding of the structure and process to give good advice. But, here are some comments/questions Why does the lecture table have fields for First Name & Last Name? Wouldn't the lectures be associated with a user or contact record? I would think (guess) the user/contact records should have the name values and the lectures table would just have a foreign key reference to that record. Then why is there a child table "lecture_subject_category"? Can't those fields be included in the lectures table? Can the subject-category combinations be different for each lecture? I.e. Is Subject A always in Category B? Or can Subject A be associated to different Catregories in different lectures. If the former is true, then the Subject/Category association should be defined in a separate table and you would only need to include the subject ID in the lectures table.
-
As Neil suggests, I think the problem is almost assuredly with the get_status() method. That code is making a request of an external server and is being called in a loop for multiple servers. You have zero control over how long an external server will take to respond (or not respond if it is offline). You can implement a timeout on the request. But, then you have to decide how long that timeout should be. Too short and it may timeout before the server gets a chance to respond and too long, well the user will be sitting there waiting. I had the same problem with an internal tool for a status page I built to query multiple application servers we use. For something like this, there are solutions. What solution you implement depends on your needs. Option 1: Caching Can you live with the results displayed being a little old (e.g. some period of time such as 5 minutes) or do they absolutely need to be right this second current. If they can be a little old, then you could implement a process as follows. 1) Create a page that queries all the servers, gets their current status, and then updates the database with that status. 2) Create a scheduled task to run that script every 5 minutes (or whatever length of time is acceptable). 3) For the page that users will access, just query the servers and their stored statuses and display them. Option 2: AJAX If you really want the data to be as current as possible when the user opens the page, I would suggest using AJAX. When the page loads, get the servers from the database and output them to the page. But. do not get their status. Instead, put a placeholder message such as "Retrieving status". After the page loads, kick off requests via JavaScript to get the status of each server (each server should be a separate asynchronous request). As each response is returned back to the JavaScript, update the status value. I have a critique on the get_status() method: public function get_status($ServerIP,&$ServerPort) { if(@stream_socket_client("tcp://$ServerIP:$ServerPort", $errno, $errstr, 5) !== false) { return "<strong style='color:#33CC00'>Online</strong>"; } else { return "<strong style='color:#CC0000'>Offline</strong>"; } } Ideally, you do not want to put the 'structure' of the output in the if/else conditions. For one, you may have a bug with one condition and not the other that becomes hard to find. Also, if you ever want to make a change on "how" the status is displayed you have to change both conditions. You should only define the differences and have one piece of code that creates the output. Well, to be really strict, the function to get the status should be completely decoupled from the process of creating the output entirely. I.e. it should only True/False and a separate process creates the output. But, I understand that sometimes doing that can seem counter-productive. But, I would at least do something like this: public function get_status($ServerIP,&$ServerPort) { $online = @stream_socket_client("tcp://$ServerIP:$ServerPort", $errno, $errstr, 5) ; //If you decouple the process of checking the status and creating the output //This function would just need to return the variable $online if($online!==false) { $statusText = 'Online'; $statusColor = '#33CC00'; } else { $statusText = 'Offline'; $statusColor = '#CC0000'; } return "<strong style='color:{$statusColor}'>{$statusText}</strong>"; }