Jump to content

php_joe

Members
  • Posts

    175
  • Joined

  • Last visited

    Never

Posts posted by php_joe

  1. Hello,

     

    I am trying to write a function that will calculate the standard deviation of values in an array. I did search past topics and used them to come up with this script:

     

    function standard_deviation($sample){
    if(is_array($sample)){
    	$mean = array_sum($sample) / count($sample);
    	foreach($sample as $key => $num) $devs[$key] = pow($num - $mean, 2);
    	return sqrt(array_sum($devs) / count($devs));
    }
    }
    

     

    It seems to work, but I get different values when I've checked it by using STDEV in OpenOffice, so I think I've made an error somewhere.

  2. Does anyone have a basic file upload script.. im trying to figure out how to code it.. and im completly lost.

     

    I know that i need to setup a form.. and i need to store the filename in a database.  so I can call the files later. Any general help on how to parse the  upload would be great.

     

    Thanks

     

     

     

    <?php

     

    //

    // This sample shows how to fill in and submit data to a form that looks like:

    //

    //  <form enctype="multipart/form-data"

    //      action="somewhere.cgi" method="post">

    //  <input type="file" name="sampfile">

    //  <input type="text" name="filename">

    //  <input type="text" name="shoesize">

    //  <input type="submit" value="upload">

    //  </form>

    //

    // Pay attention to:

    //  #1 - the input field names (name=)

    //  #2 - the input field type so that you pass the upload file to the right

    //        name

    //  #3 - what URL to send the POST to. The action= attribute sets it.

    //

    // Author: Daniel Stenberg

     

      $uploadfile="/tmp/mydog.jpg";

      $ch = curl_init("http://formsite.com/somewhere.cgi"); 

      curl_setopt($ch, CURLOPT_POSTFIELDS,

                  array('sampfile'=>"@$uploadfile",

                        'shoesize'=>'9',

                        'filename'=>"fake name for file"));

      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

      $postResult = curl_exec($ch);

      curl_close($ch);

      print "$postResult";

    }

     

    ?>

     

  3. Hi everyone!

     

    <select name="monkskin">
    <option value="0" $i>24-7GT Default</option>
    <option value="1" $i1>Light Blue</option>
    <option value="2" $i2>Sunset</option>
    <option value="3" $i3>Beautiful Blue</option>
    <option value="4" $i4>Tan</option>
    <option value="5" $i5>Crazy Gray</option>
    <option value="6" $i6>Toxin</option>
    </select>

     

    And I have to use all this php code so it stays selected! :(

    ($member['monkskin']=='0')? $i = "selected":""; ($member['monkskin']=='1')? $i1 = "selected":"";($member['monkskin']=='2')? $i2 = "selected":"";
    ($member['monkskin']=='3')? $i3 = "selected":"";($member['monkskin']=='4')? $i4 = "selected":"";($member['monkskin']=='5')? $i5 = "selected":"";($member['monkskin']=='6')? $i6 = "selected":"";
    ($member['monkskin']=='7')? $i7 = "selected":"";

     

    I know there's a way so it automatically Does it.. using foreach or something? Could you please help em out on this.. Thanks..

     

    Because I am doing alot of this and I dont like to manually do all the php code to make it stay selected..

     

    Change $i to $i0 and then use this code before the <select> tag:

     

    <?

    if($monkskin) $i$monkskin = "selected":

    ?>

  4. I was trying CURL originally, because I wanted to log in and submit an action. It timed out, so I tried file_get_contents() as a test, and it timed out too.

     

    file_get_contents() works fine for every other site that I've tried it on.

     

    The error code I'm getting says this:

     

    Warning: file_get_contents(http://shop.darkswords.ru) [function.file-get-contents]: failed to open stream: Connection timed out in /hsphere/local/home/mysite.com/testfile.php on line 2

     

    I'm wondering if it has something to do with the encoding. The site was written and hosted in Russia, and the comments are in Russian.

     

    Could there be some kind of (human) language-based incompatibility?

     

    Joe

  5. Ok, I can't figure out what I'm doing wrong. I got this simple chat script that uses mySQL and I've been trying to modify it so it's flat-file, but I just can't get it to work.

     

    The script uses 3 pages. The first is the chat window, the second is a php file that stores the recent posts, and the third writes new posts to the second file and then displays any posts that havn't been read by the viewer yet.

     

    This is the chat file:

     

    <html>
    <head>
    	<title>AJAX Driven Web Chat</title>
    	<style type="text/css" media="screen">
    		.chat_time {
    			font-style: italic;
    			font-size: 9px;
    		}
    	</style>
    <script language="JavaScript" type="text/javascript">
    var sendReq = getXmlHttpRequestObject();
    var receiveReq = getXmlHttpRequestObject();
    var lastMessage = 0;
    var mTimer;
    
    		//Function for initializating the page.
    		function startChat() {
    			//Set the focus to the Message Box.
    			document.getElementById('txt_message').focus();
    			//Start Recieving Messages.
    			getChatText();
    		}
    
    		//Gets the browser specific XmlHttpRequest Object
    		function getXmlHttpRequestObject() {
    			if (window.XMLHttpRequest) {
    				return new XMLHttpRequest();
    			} else if(window.ActiveXObject) {
    				return new ActiveXObject("Microsoft.XMLHTTP");
    			} else {
    				document.getElementById('p_status').innerHTML = 'Status: Cound not create XmlHttpRequest Object.  Consider upgrading your browser.';
    			}
    		}
    
    //Gets the current messages from the server
    function getChatText() {
    if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
    //if(typeof lastMessage=="undefined") alert("undefined");
    //else alert(lastMessage);
    receiveReq.open("GET", 'getChat.php?chat=1&last=' + lastMessage, true);
    alert('getChat.php?chat=1&last=' + lastMessage);
    receiveReq.onreadystatechange = handleReceiveChat; 
    receiveReq.send(null);
    }			
    }
    
    		//Add a message to the chat server.
    		function sendChatText() {
    			if(document.getElementById('txt_message').value == '') {
    				alert("You have not entered a message");
    				return;
    			}
    			if (sendReq.readyState == 4 || sendReq.readyState == 0) {
    				sendReq.open("POST", 'getChat.php?chat=1&last=' + lastMessage, true);
    				sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    				sendReq.onreadystatechange = handleSendChat; 
    				var param = 'message=' + document.getElementById('txt_message').value;
    				param += '&name=Ryan Smith';
    				param += '&chat=1';
    				sendReq.send(param);
    				document.getElementById('txt_message').value = '';
    			}							
    		}
    
    //When our message has been sent, update our page.
    function handleSendChat() {
    //Clear out the existing timer so we don't have 
    //multiple timer instances running.
    clearInterval(mTimer);
    getChatText();
    }
    
    //Function for handling the return of chat text
    function handleReceiveChat() {
    if (receiveReq.readyState == 4) {
    var chat_div = document.getElementById('div_chat');
    var xmldoc = receiveReq.responseXML;
    var message_nodes = xmldoc.getElementsByTagName("message"); 
    var n_messages = message_nodes.length
    for (i = 0; i < n_messages; i++) {
    var user_node = message_nodes[i].getElementsByTagName("user");
    var text_node = message_nodes[i].getElementsByTagName("text");
    var time_node = message_nodes[i].getElementsByTagName("time");
    lastMessage = (message_nodes[i].getAttribute('id'));
    chat_div.innerHTML += '<span class="chat_time">';
    chat_div.innerHTML += time_node[0].firstChild.nodeValue;
    chat_div.innerHTML += "</span> [<span class='user'>";
    chat_div.innerHTML += user_node[0].firstChild.nodeValue;
    chat_div.innerHTML += '</span>]: ';
    chat_div.innerHTML += last[0].firstChild.nodeValue + ' - ';
    chat_div.innerHTML += text_node[0].firstChild.nodeValue + '<br />';
    chat_div.scrollTop = chat_div.scrollHeight;
    }
    mTimer = setTimeout('getChatText();',2000); //Refresh our chat in 2 seconds
    }
    }
    
    //This functions handles when the user presses enter.  Instead of submitting the form, we
    //send a new message to the server and return false.
    		function blockSubmit() {
    			sendChatText();
    			return false;
    		}
    		//This cleans out the database so we can start a new chat session.
    		function resetChat() {
    			if (sendReq.readyState == 4 || sendReq.readyState == 0) {
    				sendReq.open("POST", 'getChat.php?chat=1&last=' + lastMessage, true);
    				sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    				sendReq.onreadystatechange = handleResetChat; 
    				var param = 'action=reset';
    				sendReq.send(param);
    				document.getElementById('txt_message').value = '';
    			}							
    		}
    		//This function handles the response after the page has been refreshed.
    		function handleResetChat() {
    			document.getElementById('div_chat').innerHTML = '';
    			getChatText();
    		}	
    	</script>
    </head>
    <body onload="javascript:startChat();">
    	<h2><a href="http://www.dynamicAJAX.com" style="color: #000000; text-decoration: none;">AJAX Driven Web Chat</a></h2>
    	<p id="p_status">Status: Normal</p>
    	Current Chitter-Chatter:
    	<div id="div_chat" style="height: 300px; width: 500px; overflow: auto; background-color: #CCCCCC; border: 1px solid #555555;">
    
    	</div>
    	<form id="frmmain" name="frmmain" onsubmit="return blockSubmit();">
    		<input type="button" name="btn_get_chat" id="btn_get_chat" value="Refresh Chat" onclick="javascript:getChatText();" />
    		<input type="button" name="btn_reset_chat" id="btn_reset_chat" value="Reset Chat" onclick="javascript:resetChat();" /><br />
    		<input type="text" id="txt_message" name="txt_message" style="width: 447px;" />
    		<input type="button" name="btn_send_chat" id="btn_send_chat" value="Send" onclick="javascript:sendChatText();" />
    	</form>
    </body>
    
    </html>

     

    And here is the getchat file:

     

    <?php
    
    function displayheaders(){
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); 
    header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" ); 
    header("Cache-Control: no-cache, must-revalidate" ); 
    header("Pragma: no-cache" );
    header("Content-Type: text/xml; charset=utf-8");
    }
    
    $time = explode('|', gmdate("Y|z|H|i|s"));
    $micro = number_format(microtime(), 3);
    $seconds = $time[3] . $time[4];
    $messageday = $time[0] . $time[1] . $time[2];
    $messagetime = (($seconds+$micro)*1000);
    if(!$last) $last = '000000000.0000000';
    $last = explode('.', $last);
    
    displayheaders(); //Send headers to prevent caching.
    
    $database = "./db.php";
    if(file_exists($database)) require $database;
    else die('Error: database missing.');
    
    if(isset($_POST['message']) && $message != '') { //Check to see if a message was sent.
    $msg[$messageday][$messagetime] = array("$time[2]:$time[3]:$time[4]", 'Admin', 'public', $message);
    if($msg) foreach($msg as $key1 => $value) {
    if($key1 >= $messageday) foreach($msg[$key1] as $key2 => $value) {
    if($key2 > ($messagetime - 10)) {
    $newline[$key2] = '$msg[' . $key1 . '][' . $key2 . '] = array("' . "$time[2]:$time[3]:$time[4]\", 'Admin', 'public', $value" . ');';
    }
    }
    }
    if($newline) $info = "<?php\n" . implode("\n", $newline) . "\n?>";
    $handle = fopen("./db.php", 'w');
    fwrite($handle, $info);
    fclose($handle);
    }
    
    $xml = '<?xml version="1.0" ?><root>';
    if($msg) foreach($msg as $key1 => $value) {
    if($key1 >= $last[0]) foreach($msg[$key1] as $key2 => $value) {
    if($key2 > $last[1]) {
    $xml .= "<message id='" . $key1 . '.' . $key2 . "'>";
    $xml .= "<time>". $msg[$key1][$key2][0] . "</time>";
    $xml .= "<user>". $msg[$key1][$key2][1] . "</user>";
    $xml .= "<type>". $msg[$key1][$key2][2] . "</type>";
    $xml .= "<text>". $msg[$key1][$key2][3] . "</text>";
    $xml .= "</message>";
    }
    }
    }else{
    $xml .= "<message id='" . $messageday . '.' . $messagetime . "'>";
    $xml .= "<time></time>";
    $xml .= "<user></user>";
    $xml .= "<type></type>";
    $xml .= "<text></text>";
    $xml .= "</message>";
    }
    
    $xml .= '</root>';
    echo $xml;
    ?>

     

    The messages are stored on db.php like this:

    <?php
    $msg[200812107][4525944] = array('07:45:25', 'Admin', 'public', 'Hello');
    $msg[200812107][4526944] = array('07:45:26', 'Admin', 'public', 'This is a test');
    $msg[200812107][4527944] = array('07:45:27', 'Admin', 'public', 'This is a line');
    $msg[200812107][4528944] = array('07:45:28', 'Admin', 'public', 'Goodbye');
    ?>

     

    Now, the AJAX on the main page should call something like getchat.php?last=20080429.8765476

     

    getchat.php should then display any messages with keys higher than $last.

     

    When I change the AJAX to display the "last" variable it shows up correctly, and when I open getchat.php and $_GET $last then the xml displays correctly, but the messages don't show up in the chat window...  :'(

  6. Why not just store as

    <?php
    $msg['2008.105.7.45.18.0'] = array("7:45:18", "Admin", "public", "Hello.");
    $msg['2008.105.7.45.19.0'] = array("7:45:19", "Admin", "public", "This is a test.");
    $msg['2008.105.7.45.20.0'] = array("7:45:20", "Admin", "public", "This is only a test.");
    $msg['2008.105.7.45.21.0'] = array("7:45:21", "Admin", "public", "If this were a real chat.");
    $msg['2008.105.7.45.22.0'] = array("7:45:22", "Admin", "public", "I wouldn't be talking to myself.");
    ?>
    

     

    I thought that keys couldn't be longer than 10 digits. I seem to remember trying a single key at first, but getting errors.

  7. I have a list of arrays like this:

     

    <?
    $msg[2008][105][7][45][18][0] = array("7:45:18", "Admin", "public", "Hello.");
    $msg[2008][105][7][45][19][0] = array("7:45:19", "Admin", "public", "This is a test.");
    $msg[2008][105][7][45][20][0] = array("7:45:20", "Admin", "public", "This is only a test.");
    $msg[2008][105][7][45][21][0] = array("7:45:21", "Admin", "public", "If this were a real chat.");
    $msg[2008][105][7][45][22][0] = array("7:45:22", "Admin", "public", "I wouldn't be talking to myself.");
    ?>

     

    I want to process only the arrays that follow a specified key, such as $msg[2008][105][7][45][20][0].

     

    I wrote this code, but it processes every array, not just the ones following $msg[2008][105][7][45][20][0].

     

    <?
    $last = "2008.105.7.45.20.0"; // <== the keys are year, day, hour, minute, second, and microsecond
    $last2 = explode('.', $last);
    
    if($msg){
    
    if($msg) foreach($msg as $key_year => $value1){
    if($key_year >= $last2[0]) foreach($msg[$key_year] as $key_day => $value2){
    if($key_day >= $last2[1]) foreach($msg[$key_year][$key_day] as $key_hour => $value3){
    if($key_hour >= $last2[2]) foreach($msg[$key_year][$key_day][$key_hour] as $key_min => $value4){
    if($key_min >= $last2[3]) foreach($msg[$key_year][$key_day][$key_hour][$key_min] as $key_sec => $value5){
    foreach($msg[$key_year][$key_day][$key_hour][$key_min][$key_sec] as $key_micro => $piece){
    
    if($key_sec > $last2[4]) $text .= display_text($piece);
    if($key_sec = $last2[4] && $key_micro >= $last2[5]) $text .= display_text($piece);
    
    }
    }
    }
    }
    }
    }
    
    }
    ?>

     

    What have I done wrong?

  8. I have an array with numerical key values, but some keys are missing.

     

    For example:

    <?
    $number[0] = 'zero';
    $number[1] = 'one';
    $number[2] = 'two';
    $number[20] = 'twenty';
    $number[30] = 'thirty';
    $number[40] = 'fourty';
    ?>

     

    I know I can use array_slice() but that won't return the values I want.

     

    For example array_slice($number, 2) would return "two", "twenty", "thirty", "fourty", which would be right,

     

    but array_slice($number, 10) would return nothing when what I want is "twenty", "thirty", "fourty"

     

    Do I have to use foreach() to loop through the entire array and unset the values that I don't want? Or is there a better way?

     

    Joe

  9. I have a JavaScript code on an external file that I'd like to keep private.

     

    I know that I can use htaccess to block direct access to the folder, but:

     

    1. Will that prevent them from saving the file using "Save As"?

    2. Will that prevent the index page from calling it as an external javascript file?

     

    Is there any additional methods that I can use to prevent people from viewing or saving an external javascript file?

     

    Thanks in advance,

     

    Joe

  10. I have a variable with a very large key and it gets parsed in a strange way.

     

    Here is the code:

    <?
    $line[11988600574445] = "line one";
    foreach($line as $key => $msg) echo "$key|$msg<br>\n";
    ?>

     

    and it outputs:

    -2147483648|line one

     

    If I have:

    <?
    $line[11988600574445] = "line one";
    $line[11988600942070] = "line two";
    $line[1198860109608] = "line three";
    foreach($line as $key => $msg) echo "$key|$msg<br>\n";
    ?>

     

    I end up with just one line on the page:

    -2147483648|line three

     

    Which, as you can see, is the last value in the array and that's not the right key.

     

    What's going on?

  11. hi friends,

          i want to convert one country currency to another country currency in my shopping web site without redirecting to another site.

    can u suggest some idea to do this...waiting for ur reply...

                               thank you.....

                                                                  regards

                                                                    Lalit

                                                                web developer

     

    This site has a converter that you can put on your site:

     

    http://www.xe.com/ucc/customize.php

     

    It's even free if you don't mind an advertisment.

  12. when are you going to work up the percentage? Each time a customer enters their info? Or at the end of the month?

     

    You could use the $d = ($a/$c) * 100; formula above to get each person's answers.

     

    If you placed the answers into an array as customers submitted their answers you could figure out the percentages in "real time" $month = (array_sum($d) / count($d)) * 100;

×
×
  • 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.