Jump to content

Psycho

Moderators
  • Posts

    12,157
  • Joined

  • Last visited

  • Days Won

    129

Everything posted by Psycho

  1. Or, you can do it with a regular expression function removeZeroRecords($string) { $string = ';'.$string; $string = preg_replace('/;0,[^;]*/', '', $string); return substr($string, 1); } $string = "324,3;65,6;0,17;93,10;"; echo removeZeroRecords($string); //Output: 324,3;65,6;93,10;
  2. function removeZeroRecords($string) { //Separate the string into an array on semicolon $records = explode(';', $string); //Iterate through each record in the array foreach ($records as $index => $record) { //Separate record into the first/second numbers based on comma $parts = explode(',', $record); if ($parts[0]=='0') { //Remove records that have a 0 as starting number unset($records[$index]); } } //Combine the remaining records into a strin and return it return implode(';', $records); } $string = "324,3;65,6;0,17;93,10;"; echo removeZeroRecords($string); //Output: 324,3;65,6;93,10;
  3. @darkfreaks: I think the OP is echoing a random number to the screen and then expecting the user to enter that random number into the field as a verification that the user is a human and not a spambot. So, he is not trying to prevent the user from seeing source code, just want to enforce the user to type the random number in rather than copy and paste. By the way, who is "Maj" ??? @yami007 : You could probably do something where an onkeydown has to be registered on the field, but I don't beleive spambots would even have JS enabled. I'd suggest making the field disabled by default and then enabling it onclick(). Of course anyone without JS enabled will be out of luck.
  4. I am *assuming* the members table is a table of members. If that is the case, then ratings and "watched" movies should be in separate tables (along with another table for the movie records).
  5. This page describes the problem in better detail, but the solutioin is the same as I provided. http://blog.unidev.com/index.php/2008/10/10/math-error-in-javascript/
  6. I forget why this happens, but just add a toFixed() to the result to get the correct value. Updated function: function Calculate() { with(document.forms['myform'].elements) { var subTotal = parseFloat(Qty1.value) * parseFloat(Price1.value); Cost1.value = subTotal; subtotal.value = subTotal; var totalValue = parseFloat(subtotal.value) + parseFloat(shipping.value) + parseFloat(tax1.value) + parseFloat(tax2.value) + parseFloat(tax3.value); total.value = totalValue.toFixed(2); } return; }
  7. Not sure what you are asking (either you don't want them to copy a paragraph displayed on the page or you have some verification code that you don't want copied). But, the solution is the same, which is there is nothing you can do. Yes, there are "right-click" hacks that attempt to prevent a user from usign features such as "view-source", but they do not work in that they are easily circumvented. If it is text displayed on the page you are trying to "protect" the best option, IMHO, is to make the text an image and use a watermark. It doesn't prevent anyone from using that image, but they won't be able to do so without people seeing your watermark and knowing it wasn't theirs. But, since it's text they could simply rekey that text into a text editor. And, again, if we are talking about the code, there is really nothign you can do except obfuscate the code, which only makes it more difficult for someone to assemble into a readable format. Besides, have you not benefited from other's code? Why are you being stingy?
  8. Yes, I am suggesting you use one table for ALL of the workout programs. Having mutiple tables doesn't make sense. Part of the power of a database is the ability to extract ONLY the data that you want. In this case you could query the one table with all the workout programs and filter it by goal, level, etc. In fact, I would imagine that you have some of the same workouts for different goals, levels, etc. In that case to "normalize" the database you would have one table of each unique workout. Then in separate tables you would associate each workout to goals, levels, days. as appropriate. For example, the bench press exercise could be associated with weight loss & strength trainign goals, beginner, intermediate & advanced levels, and 3 & 5 day workouts. The table structure would look like this exercises id | name | sets | reps | minutes 1 | bench press | 10 | 10 | 20 goals exercise_id | goal 1 | weight loss 1 | strength training (note: you could have a separate table to translate the goal_ids into their names or handle in the PHP code) levels exercise_id | level 1 | beginner 1 | intermediate 1 | advanced days exercise_id | days 1 | 3 1 | 5 Of course this may mean a lot of rework. So, decide if its worth it to you to take this approach. Also, if you have a field named sets, do not put "3 sets" as the value. Just use 3.
  9. To be honest, I still don't completely understand what exactly you are doing. My suggestion above was only that - it can be modified to fit your particular needs (if it makes sense). For example, you don't have to use Div1, Div2, etc. You could have descriptive names for them if need be. But, building the entire page should be straitforward as well. I guess I'm just not following "why" you need to create the text in a single line of code. Could you not do somthing like this: $responseText = '<td>'; $relatedRecords = $record->getRelatedSet("relatedSetName"); if (FileMaker::isError($relatedRecords) === false) { $master_record = $record; foreach ($relatedRecords as $record) { $responseText .= nl2br($record->getField('fieldName::Language', 0)); } $record = $master_record; } $responseText .= '<td>'; //Perform additional operations as needed and add text to the //$responseText variable to be returned through the AJAX call //Output the entire response text for AJAX response echo $responseText
  10. Yes, but... You can send back multiple responses as one response! I've done something similar in the past. So, let's say you have 10 divs on the page: div1, div2, etc. Then let's say there is a button on the page that will make an AJAX call and the response from that AJAX call needs to update one or more of the divs but the JS code won't know ahead of time how many or which ones - it will be determined when teh AJAX call is received by the server-side code and is processed. Here is one solution: When the call is made to the server-side page and logic is run to determine what needs to be returned to the JavaScript, the server-side code will determine which divs need to be updated and return a single sting that the javascript will parse and populate the appropriate divs. Here's an example: The PHP code will determine which Divs need to be updated and will return the entire set of data in this format: div1:div 1 content|div3:This content goes in div 3|div8:And of course,this goes in div8 The JavaScript will then parse the return value and populate the divs accordingly such as this // returnVal is the value returned fromt eh AJAX call //Split the return value into an array of name/value pairs for each div var divs = returnVal.split('|'); //Iterate through each div name/value pair for(var i=0; i<divs.length; i++) { //Split the div name/value pair into it's parts divParts = divs[i].split(':'); //Poulate the div with it's content document.getElementById(divParts[0]).innerHTML = divParts[1]; } This was just an example. Youwould need to make sure that whatever characters you use to deliniate the data would not be included in your data. You could always use quotes around the text, control characters, etc. Hope this helps.
  11. I've reread your post a few times and I'm wondering "why" you need to take this approach? I always appreciate compact code, but not at the expense of readability and useability. If this is server-side code to generate the response for an AJAX call you don't have to echo all of the text in one line. However, if you need to run nl2br() on more than one array, you may want to create a function to do that and return the array as a single string using implode function arrayToString($array) { foreach ($array as $key => $value) { $array[$key] = nl2br(); } return implode('', $array); }
  12. I *think* I understand your problem. Try using the ternary operator (If/Else shorthand) $var = 'foo'; $string='Start'.(($var=='foo')?'-TRUE-TEXT-':'-FALSE-TEXT-').'End'; echo $string; //Output: Start-TRUE-TEXT-End $var = 'bar'; $string='Start'.(($var=='foo')?'-TRUE-TEXT-':'-FALSE-TEXT-').'End'; echo $string; //Output: Start-FALSE-TEXT-End
  13. Having multiple files should not make a perceptible difference. I would guess there would be some additional time involved due to the renegotiation to find the file and start the transfer, but I think we're talking microseconds. If you really want to know the impact - do a test. Assign getTime() to a variable before you load the external files and then assign getTime() to another variable after you load them - then check the difference. Then for comparison, put all the JS into one external file and run the test again.
  14. Why not just send out a new temp password instead of using an unsecure method of storing the data. It's just good practice. Any site can get infiltrated. That woudl include the database and the PHP files that are used to encrypt/decrypt data in your database. Because many users use the same usernames, passwords, etc. on several sites if someone can get the info from one site they can try that info on other sites such as banking. It's just good practice, IMHO, to use an undecryptable method using a good salt.
  15. Please mark the topic as solved. And, I was not implying that you did expect me to write the code for you. I was just commenting on the fact that you, unlike many posters, took responsibility for reviewing the code that was providied and providing constructive feedback on what didn't work right. For that I thank you. Far too often I see replies from people simply stating that the code doesn't work without providing an error message, let alone actually doing the simplest of debugging.
  16. Note iarp's script will provide a dialog to Open/Save/Cancel the file instead of attempting to directly open the file (as requested by the OP). That script can be used to attempt to open the file automatically by commenting out this line: header('Content-Disposition: attachment; filename="'.$name.'"')
  17. Well, the actual solution will depend upon "how" you want to access the files. Are the file names/paths stored in a database and you will pass an ID to find the file details int he database. Or perhaps you will simply pass the file name without the path to the file so the user can't directly link to the file. Here's an example //Get the file name and path in some manner //e.g. pass an id ot name to the page such as //http://www.mydomain.com/showpdf.php?id=4 //Generate a variable with the full path and name to the file on the server $filename = "/pdffiles/mypdf.pdf"; if (file_exists ($filename)) { header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Type: application/pdf"); header("Content-Transfer-Encoding: binary"); header('Content-Length: '. filesize($filename)); readfile($filename); } else { echo "File not found"; }
  18. I typically do not test the code I post and instead consider it a "guide" to the poster (ergo the text in my sig). I appreciate the fact that you found/resolved the minor errors - typically people just post back "your code didn't work". Anyway, I'm not an AJAX expert, but I think the problem is that the original code was designed to expect XML data. After looking at a tutorial it looks like there are different ways to receive the response data. Try changing this var divContent = ajaxObject.responseXML.documentElement; To this var divContent = ajaxObject.responseText;
  19. You're making this too complicated. When you do the AJAX call simply query for the results then build the formatted output within the PHP code and deliver the formatted results back to the JavaScript to display on the page. The results of an AJAX call do not have to be XML data. function ajaxResponse() { // this function will handle the processing // N.B. - in making your own functions like this, please note // that you cannot have ANY PARAMETERS for this type of function!! if (ajaxObject.readyState == 4) // if ready state is 4 (the page is finished loading) { if (ajaxObject.status == 200) // if the status code is 200 (everything's OK) { // here is where we will do the processing var divContent = ajaxObject.responseXML.documentElement; document.getElementById('descriptionarea').innerHTML = divContent; } else // if the status code is anything else (bad news) { alert('There was an error. HTTP error code ' + ajaxObject.status.toString() + '.'); return; // exit } } // end if // if the ready state isn't 4, we don't do anything, just // wait until it is... } // end function ajaxResponse <?php // Error Display ini_set('display_errors', 1); ini_set('log_errors', 1); ini_set('error_log', dirname(__FILE__) . '/error_log.txt'); error_reporting(E_ALL & ~E_NOTICE); //Connect to Server and Select Database $con = mysql_connect("localhost","root","*******"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("phonedirectory", $con) or die ('Could not select db: ' . mysql_error());; // Start script $firstName = mysql_real_escape_string($_GET['fname']); $queryfirstname = "SELECT * FROM employee WHERE EmpFName LIKE '%{$firstName}%'"; $firstname = mysql_query($queryfirstname) or die(mysql_error()); $rows = mysql_num_rows($firstname); if (mysql_num_rows($firstname)==0) { echo "<table>"; echo "<tr><th>Last Name</th><th>First Name</th></tr>"; while($row = mysql_fetch_array($firstname)) { echo "<tr><td>{$row['EmpLName']}</td><td>{$row['EmpFName']}</td></tr>"; } echo "</table>"; } else { echo "No results found."; } ?> NOTE: Modified the query to 1) escape the user value to prevent SQL injection/data corruption and 2) use a LIKE clause so the user can find partial matches
  20. Only if you can determine how the records are related. The current code has two seperate lists of records. How are they related? As I stated, if there is a relation you "should" be able to get the data with a single query (including the difference value).
  21. Ah, yes. I was thinking about the "reading" of a file and didn't consider XML. You could, of course, read/parse an XML file with JavaScript. If there is a way to read/parse fiels with AJAX that are not XML, please elaborate. I still contend that a server-side language is the way to go due to cross-browser issue whenever dealing with javascript.
  22. Yep, byt he time you get to the math operation, you have already run through all of the records from the first query and all the records from the second query. Since you use teh same variable name for the records ($row), the value for $row1['Weight'] from teh first query is wiped out when you get to the calculation. If you were to use different variable names in those while loops you could get the difference between the LAST weight and the LAST score using something like: $diff=($row2['Score'] - $row1['Weight']); But, I'm not sure that is what you are trying to accomplish. Are you trying to get the difference of the total scores and the total weights? That seems doable. but, if you are wanting the difference on a record by record level, I don't know how the records are supposed to match up. If there is some way to correlate the records you could get it all with one query.
  23. The '===' means an identical comparison - not just evaluated equal. In other words, the values are just not "evaluated" as equal - they are exactly identical. A few examples should explain clearly: if (1 == true) //result is true (a 1 is evaluated as true) if (1 == 1) //result is true if (1 === true) //result is false (they are not the same type) if (1 === 1) //result is true This PHP manual page should help as well: http://us3.php.net/operators.comparison As for the code tags, you can simply select the PREVIEW button to get your post in a form where you can insert them with the available tools, or just put these around your code (without spaces) and it will display like my examples above: [ c o d e ]code goes here[ / c o d e ]
  24. Ok, I've made a lot of changes. You should really give variables descriptive names - not group1, group2, etc. I used 'goal', 'level' and 'days'. I also changed the values to values you can use directly in the code without a lot of IF/ELSE statements. I noticed that all of the queries were using a table with 'beg' in the name and there were no intermediate tables (I assumed that was a mistake and that there are tables with 'int' in the name). Although, I rewrote the code using mutiple tables as you had in the original code, I will state that that is a terrible idea. Simply create one table with additional fields for goal, level & days. You can then query that one table using those fields as criteria. Using multiple tables is very poor design in this case. Note: I did not test any of this, so there are probably some syntax errors New Form <form action="weightloss_beg_3day_exercise.php" method="POST"> <font color="#D3D3D3" size="2" face="Verdana"><b>What is your goal?</b></font> <table border="1" width="55%" height="70%" cellspacing="1" cellpadding="1"> <tr> <td><font color="#D3D3D3" size="2" face="Verdana"><b>Weight Loss:</b></font></td> <td border="1" width="30%" height="20%" cellspacing="1" cellpadding="1"><input type="radio" name="goal" value="weightloss" CHECKED></td> </tr> <tr> <td><font color="#D3D3D3" size="2" face="Verdana"><b>Tone:</b></font></td> <td><input type="radio" name="goal" value="tone"></td> </tr> <tr> <td><font color="#D3D3D3" size="2" face="Verdana"><b>Muscle Building:</b></font></td> <td><input type="radio" name="goal" value="musclebuilding"></td> </tr> </table><br><br> <font color="#D3D3D3" size="2" face="Verdana"><b>Are you a/an?</b></font> <table border="1" width="55%" height="70%" cellspacing="1" cellpadding="1"> <tr> <td><font color="#D3D3D3" size="2" face="Verdana"><b>Beginner:</b></font></td> <td border="1" width="30%" height="20%" cellspacing="1" cellpadding="1"><input type="radio" name="level" value="beg" CHECKED></td> </tr> <tr> <td><font color="#D3D3D3" size="2" face="Verdana"><b>Intermediate:</b></font></td> <td><input type="radio" name="level" value="int"></td> </tr> </table><br><br> <font color="#D3D3D3" size="2" face="Verdana"><b>How many days can you train per week?</b></font> <table border="1" width="55%" height="70%" cellspacing="1" cellpadding="1"> <tr> <td><font color="#D3D3D3" size="2" face="Verdana"><b>3 Days:</b></font></td> <td border="1" width="30%" height="20%" cellspacing="1" cellpadding="1"><input type="radio" name="days" value="3" CHECKED></td> </tr> <tr> <td><font color="#D3D3D3" size="2" face="Verdana"><b>4 Days:</b></font></td> <td><input type="radio" name="days" value="4"></td> </tr> <tr> <td><font color="#D3D3D3" size="2" face="Verdana"><b>5 Days:</b></font></td> <td><input type="radio" name="days" value="5"></td> </tr> <tr> <td align="center"><input type="submit" name="Submitcreateworkout" value="Get Program"></td> </tr> </table> </form> New PHP <?php if (isset($_POST['Submitcreateworkout'])) { $query = "SELECT * FROM {$goal}_{$level}_{$days}day_exercise"; $result = mysql_query($query) or die (mysql_error()); $day = 1; while ($row = mysql_fetch_array($sql_result)) { echo "<b>Your weightloss program for day {$day} is:</b><br><br>"; echo "<table width =\"50%\ align =\"center\" border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"font-size: 70%\">\n"; echo " <tr>\n"; echo " <td width =\"5%\ height =\"5%\ border=\"1\" cellpadding=\"1\" cellspacing=\"1\">\n"; echo " <a href=\"{$row['link']}.php\">{$row['name']}</a>"; #echo " <a href=\"popup(".$row['link'].".php)\">".$row['name']."</a>"; #echo " <A HREF=\".$row['link']." onClick=\"return popup(".$row['link'].".php)"\>".$row['name']."</A>"; echo " </td>\n"; echo " <td width =\"5%\ height =\"5%\ border=\"1\" cellpadding=\"1\" cellspacing=\"1\">{$row['sets']}</td>\n"; echo " <td width =\"5%\ height =\"5%\ border=\"1\" cellpadding=\"1\" cellspacing=\"1\">{$row['reps']}</td>\n"; echo " <td width =\"5%\ height =\"5%\ border=\"1\" cellpadding=\"1\" cellspacing=\"1\">{$row['duration']}</td>\n" echo " </tr>"; echo "</table><br>\n"; } } ?> Wow, look how much smaller that is. If you were to go with one table, as I think you really should, the query would look something like this: $query = "SELECT * FROM programs WHERE goal='$goal' AND level='$level' AND days='$days'"; Much, much easier to maintain.
  25. 1. Use code tags when positing code. makes it MUCH easier to read. 2. Are you kidding me with all those if/else's 3. All of your IF statemetns are if (isset($_POST['Submitcreateworkout'])) { The submit button is the field named 'Submitcreateworkout', so that IF will always return true and only the first one will run since all of the others are ELSE clauses. I'll take a stab at correcting the code.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.