Psycho
Moderators-
Posts
12,157 -
Joined
-
Last visited
-
Days Won
129
Everything posted by Psycho
-
Did you see my response? The code you're posting can't be working. I tested it and there is a parse error because there is no semicolon after $result = mysql_query($sql3, $con) When I ran the same code (modified for a test DB) I get this error Parse error: parse error, unexpected T_VARIABLE in C:\xampp\htdocs\test\test.php on line 14 (14 being the line after the line with the missing semi-colon, as expected) So, I don't see how what you posted is the actual code you are using. Did you try the code I posted (which works for me)?
-
Best Practice on How to Structure PHP/HTML Code?
Psycho replied to limitphp's topic in PHP Coding Help
There is no ONE best practice. Don't know exactly what you mean by some pages need the same variables. Are these "satic" in that they are the exact same values for each and every user (in that case use a config file that is included on all pages) or are these values static for each session/user (in that case use $SESSION). It all depends on the implementation. -
Interacting with PHP from JavaScript - Multiple Selects
Psycho replied to tmyonline's topic in PHP Coding Help
You're looking for an AJAX solution. Too much to try and detail in a forum post. Many tutorials out there. There is an AJAX forum here if you run into trouble. -
You shouldn't even include the ORDER_ID in the insert statement at all. Also, that is the oddest use of an "or die()" [on line all by itself, previous line was ended with a semi-colon] I have ever seen. I don't even know how that would process. But it could be the fact that you have your "$order_ID = mysql_insert_id()" sandwiched in-between the query and the "or die()" that is causing the problem. Try this: require "connect.php"; $sql3="INSERT INTO `kf`.`order` (`cust_id`,`order_date`,`order_status`,`order_total`) VALUES ('{$cust_id}','{$today}','{$status}','{$order_tot}')"; $result = mysql_query($sql3, $con) or die ("Unable to perform query"); $order_ID = mysql_insert_id(); echo "$order_ID"; Also, pleas use CODE or PHP tags when posting code.
-
I'll have to disagree with you permiso. Strictly speaking you can read the PDF file contents, but it will consist of human unreadable code. Such as this: ©?}!&ŠÞáQS—×=K$ÀpF½ÁñξaG_Äßý#ÎþQ{ÿCÄî‹8û¢®txÇ ½ÑNoÔåqÑ There's no strait-forward way to read the displayed words in a PFD file. You would probably want to get a 3rd party application that you can run on the server (assuming you have the rights to do so) which PHP can utilize. Here is one possibility where a person stated that you could use it to get the text from a PFD without any formatting: xpdf: http://www.foolabs.com/xpdf/ Or PDFTOTEXT: http://www.pdf2text.com/ConvertPDFToText-server-edition.htm
-
[SOLVED] eregi? or is there something better..
Psycho replied to origaflux's topic in PHP Coding Help
Make the regular expression "non greedy" with the question mark eregi("<!--.*?-->", $post); -
corbin was asking what errors do you get if you go to the file directly. I believe he means if you go to the JS file directly, i.e. "www.yourdomain.com/js/toggle.js"? Is the file found by the browser?It should attempt to download it. Could be a caching issue where it has "remembered" the location of the fine and can't find it any more. Try pressing Ctrl-F5 while on the html page. however, I did not ice in your last post you put this: That's not the same as "/templates/admin/js/toggle.js". Typo? Also, what is the path to the HTML file that is loading the JS file? And, i will admit I have had similar bouts of frustration and found the problem was just a bonehead issue on my part such as I was editing my local copy of the HTML page and had moved the JS file on my live server.
-
Or better yet, create a really cool flash navigation menu that makes the use wait 30 seconds before they can actually navigate to another page. Ah, the good old days of the <BLINK> tag, where have you gone?
-
<html> <head> <script type="text/javascript"> function addText (fieldID, textStr) { document.getElementById(fieldID).value = textStr; } </script> </head> <body> <img src="image.jpg" onclick="addText('foo', '<b>bar</b>');"><br> Field Foo: <input type="text" name="foo" id="foo"> </body> </html>
-
I second that. A hash plus a good salt is sufficient.
-
The only solution I can think of would be to use (ick!) frames. Have the site load in a parent document that loads two frames - one for the music to play in and one for the content.
-
The results should be posted in a multidimensional array such that each sub-module is in a different dimensions. Top level would be module, next level would be submodule1, next is submodule2, etc.
-
//Shorthand if/else to set the value of $checked to // checked="checked" or empty string $checked = ($feat == "Yes") ? ' checked="checked"' : ''; echo "<input type=\"hidden\" name=\"pic[{$counter}]\" value=\"{$pic}\" /> <input type=\"text\" name=\"comment[{$counter}]\" value="{$qry['comment']}\" /> Featured:<input type=\"checkbox\" name=\"featured[{$counter}]\" value=\"Yes\"$checked /></td>";
-
[SOLVED] Average time using 00:15:01 type of time
Psycho replied to coolbeansdude51's topic in PHP Coding Help
Hmm.. Those are some odd results. But, looking at the code you posted I'm seeing some errors and odd logic. First off, there is the error I alluded to earlier $i = 0; $stop = count($friends->status); while ($i <= $stop) { If there are 10 records and the first record is at index 0, then our loop should continue until index number 9 (0-9 is 10 elements). So the while loop should be "($i < $stop)" not "($i <= $stop)" which will go from 0 to 10, i.e. 11 elements. Next you have this code $times[] = explode(' ',$friends->status[$i]->created_at); You state that $friends->status[$i]->created_at is an array, but you use explode on it? Even if we assume it is a string that you then convert to an array, you are then assigning that array to $times[], but then you only process $times[3]. I *think* what you want to be processing is $times[n][3], where n represents each record. Give this a try <?php $i = 0; $record_count = count($friends->status) while ($i < $record_count) { $record = explode(' ', $friends->status[$i]->created_at); $thisTime = $record[3]; if (!echo preg_match('/^\d{1,2}:\d{1,2}:\d{1,2}$/', $thisTime) { echo "Invalid time: [$thisTime], record $i<br>"; } else { $times[] = $thisTime; } $i++; } function averageTime($timesArr) { foreach($timesArr as $timeStr) { //Convert to seconds and add to array $seconds[] = hms2sec($timeStr); } return sec2hms(round(array_average($seconds))); } function hms2sec($time) { //Split time into parts $timeParts = explode(':', $time); $seconds = ($timeParts[0]*3600 + $timeParts[1]*60 + $timeParts[2]); //echo "hms2sec: Input ($time), Output ($seconds}<br>"; return $seconds; } function sec2hms($seconds) { $hours = intval($seconds/3600); $minutes = intval($seconds%3600/60); $seconds = ($seconds%60); $hms = sprintf("%s:%02s:%02s", $hours, $minutes, $seconds); echo "sec2hms: Input ($seconds), Output ($hms}<br>"; return $hms; } function array_average($array) { return (array_sum($array)/count($array)); } echo averageTime($times); ?> For further debugging, hard code the value of $record_count to be something like 10 and then uncomment the other debuggin line in the hms2sec() function. I wouldn't have that line uncommented if you are running thousands of records. -
Although you seem to have a solution, I think there may be a better one. Simply create a while loop that will continue indefinitely (with an exit trigger) as long as the script is unable to change the name of the text file. So, the first script in will change the name and the second script in will wait until the name is changed back. Here is some mock code (not tested). At the very least you need to first put a sleep() call within your while loop as the server will put as much CPU cycles as it can to run that loop. If there was ever a problem with the file that it simply got fubar'd and couldn't be opened you could crash the server. Which is why you should also implement a flag to exit the loop after n iterations to prevent an indefinite loop. $loops = 0; $file_ready = true; //Flag to determine if file is available while (!rename('/path/names.txt', '/path/temp_names.txt')) { //After 5 attempts exit the loop if ($loops==5) { $file_ready = false; break; } //Wait one second before trying again sleep (1); } if ($file_ready) { echo "Unable to access file"; } else { //Read and write contents to file then rename back to original name rename('/path/temp_names.txt', '/path/names.txt'); }
-
[SOLVED] Average time using 00:15:01 type of time
Psycho replied to coolbeansdude51's topic in PHP Coding Help
<?php function averageTime($timesArr) { foreach($timesArr as $timeStr) { //Convert to seconds and add to array $seconds[] = hms2sec($timeStr); } return sec2hms(round(array_average($seconds))); } function hms2sec($time) { //Split time into parts $timeParts = explode(':', $time); return ($timeParts[0]*3600 + $timeParts[1]*60 + $timeParts[2]); } function sec2hms($seconds) { $hours = number_format(intval($seconds/3600), 0); $minutes = str_pad(intval($seconds%3600/60),2, '0', STR_PAD_LEFT); $seconds = str_pad($seconds%60,2, '0', STR_PAD_LEFT); return "$hours:$minutes:$seconds"; } function array_average($array) { return (array_sum($array)/count($array)); } $times = array('1:10:00', '1:05:00', '2:55:00'); echo averageTime($times); ?> Will output 1:43:20 (which is correct). -
[SOLVED] Average time using 00:15:01 type of time
Psycho replied to coolbeansdude51's topic in PHP Coding Help
I'm not sure how your 'array' is constructed, but when I tested your code it created an empty element at the end of each record - skewing the results (because of the <= in the while loop). Even after I corrected for that, the code still calculates incorrectly. Using the times 1:10:00 & 1:05:00 & 2:55:00 it calculated the average as 1:23:0. But, if you convert the times to minutes you get: (70 + 65 + 175) = 310 / 3 = 103 minutes averaged. 103 minutes is 1:43:00. Give me a sec and I'll give you some code that works -
[SOLVED] Form validation, submitting before script finishes.
Psycho replied to atrum's topic in Javascript Help
I don't believe you can use two variables in a with() statement. -
Might be an easier way, but this will work. You can use it to covnert back and forth. Whatever format you are inputting you will get the opposite out. function reverseDayMonth(dateStr) { dateArr = dateStr.split('/'); return dateArr[1]+'/'+dateArr[0]+'/'+dateArr[2]; }
-
Don't take this the wrong way, but that code is not very organized. There is absolutely no formatting to show logical flow and there is a ton of unnecessary code. That makes it difficult for anyone to analyze the code and provide help. Fortunately I was bored and didn't feel like working today. This will do what you want, but for some reason the names for the sub fields at levels 2 & 3 are getting some odd numbers in the indexes for their names. (I created all the input fields with names that will be parsed as arrays when POSTed) But, the names are in a logical order that you can parse. <html > <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <script type="text/javascript"> //======================================== // BEGIN SUPPORTING FUNCTIONS //======================================== function divObj(level) { var divObj = document.createElement('div'); divObj.addBR = _addBR; divObj.addButton = _addButton; divObj.addLabel = _addLabel; divObj.addInput = _addInput; divObj.style.marginLeft = (level*20)+'px'; divObj.style.whiteSpace = 'nowrap'; return divObj; } function _addLabel(text, style) { textObj = document.createTextNode(text); fontObj = document.createElement("font"); fontObj.setAttribute('style', style); fontObj.appendChild(textObj); this.appendChild(fontObj); return; } function _addInput(name, style) { var inputObj = document.createElement('input'); inputObj.setAttribute('type', 'text'); inputObj.setAttribute('style', style); inputObj.setAttribute('size', '80'); inputObj.setAttribute('name', name); this.appendChild(inputObj); return; } function _addBR() { this.appendChild(document.createElement('br')); return; } function _addButton(text, action) { var bttnObj = document.createElement('button'); bttnObj.setAttribute('type', 'button'); bttnObj.setAttribute('style', 'margin-left:5px;'); bttnObj.setAttribute('value', ''); bttnObj.onclick = new Function(action); bttnObj.appendChild(document.createTextNode(text)); this.appendChild(bttnObj); return; } function insertAfter(referenceNode, newNode) { referenceNode.parentNode.insertBefore( newNode, referenceNode.nextSibling ); } function nodeIndex(nodeObj) { for (var i=0; i<nodeObj.parentNode.childNodes.length; i++) { if (nodeObj.parentNode.childNodes[i]==nodeObj) { return i; } } } //======================================== // END SUPPORTING FUNTIONS //======================================== function addModule(modElem) { var newDivObj = new divObj(0) newDivObj.addBR(); newDivObj.addLabel('Module: ', 'color:#000066;font-size:10pt;font-weight:normal;'); newDivObj.addInput('module[]'); newDivObj.addButton('+', 'addSubModule(this, 1)'); newDivObj.addButton('-', 'removeElement(this)'); newDivObj.addBR(); newDivObj.addButton('ADD', 'addModule(this)'); newDivObj.addBR(); insertAfter(modElem.parentNode, newDivObj) return; } function addSubModule(modElem, level) { var parentObj = modElem.parentNode; var inputName = ''; switch(level) { case 3: subModule2ID = nodeIndex(parentObj); parentObj = parentObj.parentNode; inputName = '['+ subModule2ID +']' + inputName; case 2: subModule1ID = nodeIndex(parentObj); parentObj = parentObj.parentNode; inputName = '['+ subModule1ID +']' + inputName; case 1: moduleID = nodeIndex(parentObj); inputName = 'subModule['+ moduleID +']' + inputName; break; } var newDivObj = new divObj(level); newDivObj.addLabel('Sub Module (lev '+level+'): ', 'color:#000066;font-size:10px;font-weight:normal;'); newDivObj.addInput(inputName+'[]'); if (level<3) { newDivObj.addButton('+', 'addSubModule(this, '+(level+1)+')'); } newDivObj.addButton('-', 'removeSec(this)'); newDivObj.addBR(); modElem.parentNode.appendChild(newDivObj); } </script> </head> <body > <form method="POST" name="form1" action="form_dyn.php" > <table width="80%" border="0" cellspacing="0" cellpadding="0"> <tr> <td> <div id="modules" width='100%'> <div id="module_1"> Module: <input type="text" name="module[]" id="module_1" /> <button style="margin-left: 5px;" type="button" onclick="addSubModule(this, 1);">+</button> <button style="margin-left: 5px;" type="button">-</button> <br> <button value="" style="margin-left: 5px;" name="" type="button" onclick="addModule(this)">ADD</button> </div> </div> </td> </tr> </table> </form>
-
[SOLVED] php to create html help? newbie here!
Psycho replied to DeeDee2010's topic in PHP Coding Help
Absolutely no need to create files for this! Simply use a database where you store records containing ID, Name, Address, information about product, etc. Then have the user navigate to http://www.mysite.com/neworder.php?id=SSFFD242 Then that page will get the record from the database for that ID and then display the "template" with that record's data. -
It would be much easier if you gave an exampe of the code you want to use to display these in. You say you wnat 10 names in each table, but did you mean 10 names in each row of a table? Anyway here is a quick example of how to do it with separate tables (not tested): <?php $count = 1; foreach ($names as $name) { if ($count==1) { echo "<table><tr>"; } echo "<td>$name</td>"; $count++; if ($count>10) { echo "</tr></table>"; $count = 1; } } if ($count!=1) { echo "</tr></table>"; } ?>
-
getimagesize() only works sometimes?! Please help
Psycho replied to br3nn4n's topic in PHP Coding Help
I think I see the error. You first get the width & height of the image and store the values in $width & $height list($width, $height) = getimagesize($filename); Then you have this IF statment that will set the values of $new_width and $new_height if ($width > $height) { $new_width=350; $new_height=($height/$width)*350; } Then later in the code you attempt to use additional image functions using $new_width & $new_height. But, if the IF statment was not true (i.e. The width is not greater than height) then the variables $new_width & $new_height are never defined! -
Sorry, but your "question" is not clear. You say you have created the code needed to convert an integer to a hexadecimal value. Then you ask "do i need to write mutlitple if functions to check through the array". What are you doing with the array? I guess I will assume that the array is filled with integer values that you want converted to hexadecimal. Do you want those values repopulated back into the array or just echo'd to the page? Since you didn't provide the code you created for converting the value I can't tell you how to utilize it with your array (however, I'm guessing you created something that was not needed - I'm sure there is an easy way in PHP to do the conversion automatically). Without knowing the structure of your array I can't be sure, but you would probably wwant to do something like this foreach ($array as $intValue) { echo conversionFunction($intValue); }
-
Your code works fine for me. I used the code you provided and created a test.php file, then created a (users.db.php) file with the content you showed. The output I received when running the test php file is Id Username Password Permissions Last Logged In You should get an error if the file name or path are incorrect. Are you poistive you don't have two versions of users.db.php floating around? Try running just this code to ensure you are reading the file $users = file("users.db.php"); $lineno = 1; foreach ($users as $line) { $user = explode('|', $line); echo "Line $lineno: $line\n"; $lineno++; }