Search the Community
Showing results for tags 'beginner'.
-
I dont understand arrays in PHP and its necessary to finally fess up and ask for help. It seems so trivial,, but I've spent Whole DAYS trying to make sense of this. (I came from assemble language programming). I am receiving UDP packets of a known size. in this case, 4 bytes of binary data. It gets unpacked ( I dont know why. I cut and pasted this simple parser from an example). All I want is the array[0] byte. I'm trying to avoid complex loops and pointers. // First lets grab a data packet and put our few bytes into the variable $buf while(1) { socket_recvfrom($socket, $buf, 16, 0, $from, $port); // Easy! but the UDP data in $buf is binary not useful like this. //Convert to array of decimal values $array = unpack("C*", $buf); // 'C' tells unpack to make unsigned chars from the binary. //Convert decimal values to ASCII characters: $chr_array = array(); // wait a second. What is happening here? for ($i = 0; $i < count($array); $i++) { $chr_array[] = chr($array[$i]); // Whats going on here? Anyone? } // So now I can assume the first byte of this $chr_array[] is my prize, right? $mychr = chr_array[0]; // Nope. Looking at $chr_array[0] either reveals a zero or the actual word "Array", depending on how late I've stayed up. print_r($array); // This is my debugger. It shows SOMETHING is working. echo "Received $mychr from remote $address $from and remote port $port" . PHP_EOL; } socket_close($sock); Output of above code... assuming the value read was 60 decimal: Array ( [1] => 60 [2] => 0 [3] => 0 [4] => 0 ) Received 0 from remote address nnn.nnn.nnn.nnn and remote port 8888 So print_r knows what to do, the last line of my code doesn't. Any help would be appreciated, as this goes to the heart of my issue of being mystified by how PHP makes and processes arrays. Its clearly not just a string of numbers. I'm trying to pull ONE printable number from that buffer. Halp!
-
Hello :-) This PHP code: <html> <?php echo "<h2>PHP is Fun!</h2>"; echo "Hello world!<br>"; echo "I'm about to learn PHP!<br>"; echo "This ". "string ". "was ". "made ". "with multiple parameters."; ?> </html> outputs this: PHP is Fun!"; echo "Hello world! "; echo "I'm about to learn PHP! "; echo "This ". "string ". "was ". "made ". "with multiple parameters."; ?> And I cannot figure out why :-(
-
I’m trying to construct a button that simply writes an "aleph" character into a text area, see below. My code does not work, can anyone tell me why ? How should I fix it ? <!DOCTYPE html> <html> <meta charset="UTF-8"> <head> <title>Example</title> <script type="text/javascript"> //JavaScript code goes here function insertAtEnd(text) { var theArea = document.getElementById("thisArea"); theArea.value += '' + text + '';; } </script> </head> <body> <input type="button" id="aleph" name="aleph" value="Write an aleph" onClick="javascript:insertAtEnd(\'<span>א</span>\');return(false)" /> <textarea id="thisArea"> </textarea> </body> </html>
-
So I started a blog project just to help me out with learning php. This is my post form <form action="insert.php" method="post"> Title: <input type="text" name="title"> <br> Post: <input type="text" name="post"> <br> Author: <input type="text" name="author"> <br> <input type="submit"> </form> Insert.php <?php $con = mysqli_connect("localhost","test","","test"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } // escape variables for security $title = mysqli_real_escape_string($con, $_POST['title']); $content = mysqli_real_escape_string($con, $_POST['post']); $author = mysqli_real_escape_string($con, $_POST['author']); $sql="INSERT INTO article (title, content, author) VALUES ('$title', '$content', '$author')"; if (!mysqli_query($con,$sql)) { die('Error: ' . mysqli_error($con)); } echo "1 record added"; mysqli_close($con); ?> But when I try to display that information here: <h1>Title: </h1> <?php echo $title; ?> <h2>Content: </h1> <?php echo $content; ?> <h3>Posted by: </h1> <?php echo $author; ?>It doesn't work and I get this: ( ! ) Notice: Undefined variable: title in C:\wamp\www\test\index.php on line 33 Call Stack # Time Memory Function Location 1 0.0000 239144 {main}( ) ..\index.php:0 Content: ( ! ) Notice: Undefined variable: content in C:\wamp\www\test\index.php on line 34 Call Stack # Time Memory Function Location 1 0.0000 239144 {main}( ) ..\index.php:0 Posted by: ( ! ) Notice: Undefined variable: author in C:\wamp\www\test\index.php on line 35 Call Stack # Time Memory Function Location 1 0.0000 239144 {main}( ) ..\index.php:0 The form works because when I looked at the database, the information was there. The problem is getting that information and displaying it in the right place, how can i fix that? Just in case, this is my index: <?php include ('connect.php'); include ('header.php'); ?> <div id="container"> <div id="rightcol"> <form action="insert.php" method="post"> Title: <input type="text" name="title"> <br> Post: <input type="text" name="post"> <br> Author: <input type="text" name="author"> <br> <input type="submit"> </form> </div> <div id="content"> <h1>Title: </h1> <?php echo $title; ?> <h2>Content: </h1> <?php echo $content; ?> <h3>Posted by: </h1> <?php echo $author; ?> </div> </div> <?php include "footer.php"; ?> </div>
-
hi there i am a beginner in PHP and i really would like some help with this..... i need to make use of the date() function to retrieve the current date. Use the split() function to retrieve the day month and the year from the current date. and the calculations to display the age. if anyone could help me with this it would be amazing. thank you!! newagecalc.php
-
another beginners question from yours truly but i was just wondering how i would code a character limit on one of my fields iv been around the internet but i cant find what i need cause i usually come up with the same error code each time. if anyone knows what to do that would be great, hears the code iv been using. <?php $con = mysqli_connect("localhost","root","","nib"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } // define variables and set to empty values $companyname = $firstname = $address1 = $address2 = $area = $city = $postcode = $email = $website = $clubphone = $homephone = $mobilephone = $typeofbusiness = $renewaldate = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { $errors = array(); if (empty($_POST["companyname"])) { $errors['companyname'] = "Please Enter Your companyname"; } <input type='text' name='keywords' size='60' value='companyname' maxlength='10' /> elseif(strpos($_POST['companyname'], '/') !== false) { $errors['companyname'] = 'Cannot use /'; } elseif(strpos($_POST['companyname'], 'The') !== false) { $errors['companyname'] = 'Company Names cant start with The'; } elseif(strpos($_POST['companyname'], 'the') !== false) { $errors['companyname'] = 'Company Names cant start with the'; } elseif(strpos($_POST['companyname'], 'THE') !== false) { $errors['companyname'] = 'Company Names cant start with THE'; } else { $companyname = test_input($_POST["companyname"]); } and heres the error code ive been getting. ( ! ) Parse error: syntax error, unexpected '<' in C:\wamp\www\AddLeads\addeadstemplate.php on line 29 aswell as this if i remove this symbol i get one to do with the else if statement witch i kinda need. again if anyone has a solution please get back to me soon as
-
Hi, Last May I created a website that is dedicated to music reviews and news. After a few months, the site was getting big, we had a large database and we were being noticed by many labels, bands and PR companies. In September last year, a student offered to help me with a php design that would make the management of my site easier and automated. He had initially scheduled a deadline for February. However, he doesn't seem in too much of a hurry to get the site up and running, and we've now lost most of our reputation. Last night, after another day where the guy made me believe he had worked hard to change a couple of colours and some format in CSS, but without managing to make the site look any better, I decided to tackle the CSS side of things myself, having built my own site six years ago, I was surprised at how much I still remembered. Anyhow, I'm in no way knowledgeable in php and there are several issues that seem like they would be very easy to solve if I knew how. I would wait for the guy to do it, but he's never in a hurry and we need the site up and running again. My first question would be the following. On the album review page (http://therealmusic.net/reviews/Jackleg-Devotional-to-the-Heart-2013) the release date should only show the month and the year, not the day. I've been asking the guy to sort this out for months now, but he always make it out to be a big deal. Here's the code I have, what do I need to add so it only extracts the month and year? <?php echo "<b>Released:</b> " . $albumreleasedate . "</br>". "<b>Genre:</b> " . $genrename . "</br>". "<b>Label:</b> " . $labelname; ?> Many thanks
-
Why does my following code display "It worked!" in the browser? <?php $a = 6; $b = 1; $c = $a + $b; ?> <?php if ($c = 3) {echo "It worked!";} else {echo "It didn't work :(";} ?>
-
Good afternoon, I am able to retrieve results from yahoo with my API key, using the instructions found here: http://developer.yahoo.com/boss/search/boss_api_guide/codeexamples.html# Code: <?php require("OAuth.php"); $cc_key = "your consumer key here"; $cc_secret = "your consumer secret here"; $url = "<http://yboss.yahooapis.com/ysearch/news>,web,images"; $args = array(); $args["q"] = "yahoo"; $args["format"] = "json"; $consumer = new OAuthConsumer($cc_key, $cc_secret); $request = OAuthRequest::from_consumer_and_token($consumer, NULL,"GET", $url, $args); $request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, NULL); $url = sprintf("%s?%s", $url, OAuthUtil::build_http_query($args)); $ch = curl_init(); $headers = array($request->to_header()); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $rsp = curl_exec($ch); $results = json_decode($rsp); print_r($results); ?> However the output of results is very bizarre, for example, if I search for elephant, I get the following results (partial copy and paste): stdClass Object ( [bossresponse] => stdClass Object ( [responsecode] => 200 [images] => stdClass Object ( [start] => 0 [count] => 35 [totalresults] => 108000 [results] => Array ( [0] => stdClass Object ( [clickurl] => http://0.tqn.com/d/goafrica/1/0/Y/Q/IMG_1592.JPG [size] => 2.2MB [format] => jpeg [height] => 2432 [refererclickurl] => http://goafrica.about.com/od/africanwildlife/ss/The-Big-5-Images-Facts-And-Information-About-Africas-Big-Five_2.htm [refererurl] => http://goafrica.about.com/od/africanwildlife/ss/The-Big-5-Images-Facts-And-Information-About-Africas-Big-Five_2.htm [title] => African Elephant - One of the "Big Five" - African Elephant Image ... [url] => http://0.tqn.com/d/goafrica/1/0/Y/Q/IMG_1592.JPG [width] => 2591 [thumbnailheight] => 150 [thumbnailurl] => http://ts2.mm.bing.net/th?id=H.4700108340594617&pid=15.1&H=150&W=160 [thumbnailwidth] => 160 ) [1] => stdClass Object ( [clickurl] => http://images.nationalgeographic.com/wpf/media-live/photos/000/004/cache/african-elephant_435_600x450.jpg [size] => 54.1KB [format] => jpeg [height] => 450 [refererclickurl] => http://animals.nationalgeographic.com/animals/photos/elephants/ [refererurl] => http://animals.nationalgeographic.com/animals/photos/elephants/ [title] => Elephant Pictures - National Geographic [url] => http://images.nationalgeographic.com/wpf/media-live/photos/000/004/cache/african-elephant_435_600x450.jpg [width] => 600 [thumbnailheight] => 120 [thumbnailurl] => http://ts2.mm.bing.net/th?id=H.5058106677789309&pid=15.1&H=120&W=160 [thumbnailwidth] => 160 ) [2] => stdClass Object ( [clickurl] => http://www.splendidwallpaper.com/wp-content/uploads/2010/03/baby_elephant_1024x768.jpg [size] => 149.4KB [format] => jpeg [height] => 768 [refererclickurl] => http://www.splendidwallpaper.com/baby-elephant-2102/ [refererurl] => http://www.splendidwallpaper.com/baby-elephant-2102/ Please bear in mind that I am very new to php, and programming in general. I am completing a fast track learning course where we don`t have time to learn the basics, we are in the deep end! Does anyone have suggestions on how to display the results properly? Thanks! Note: I had tried to do it this way (which worked with the Google API - although the links were not clickable).. It did not work at all with Yahoo: foreach ($results->{ 'items' } as $item ) { echo $item->{ 'title' }.": ".$item->{ 'link' }."\n\n"; echo $newline; }
-
Hi Guys, I'm new here and just started learning PHP a couple of days ago. I have to say it's not nearly as intense as I thought it would be (coming from the simple html/css world) Anyways, I'm at the point where i'm learning about the /n line break. I was testing it in dreamweaver and saw that none of the browsers (Firefox, Safari, Chrome, etc.) recognize it, which makes perfect sense since browsers don't understand the PHP language. So I was wondering, when and where is the \n line break useful? I know you can spot the break if you check the source code from the browser, but other than that, there is no change on the front-end. Thanks guys!
-
How doi do this in php Please help foreach account if adfundnotificicationdata exists if adfund is lower than threshold && current notificationdata is higher than threshold sendmail... update notificationdata... else insert new data
-
I've never taken a logic class, or a CS class. This is likely very easy to anyone with any background in either topic. I have an array, $_SESSION['thresholds'], where I've given the array keys values such as "thresholdA", "thresholdB", "thresholdC", "thresholdD", etc. with corresponding elements that increase in value (5,15,50,75,etc.). They array is currently mapped in ascending order. I have a variable, $_SESSION['compare'], that I want to compare against all the elements in the $_SESSION['thresholds'] and, ultimately, I want to be able to echo/print the key of the largest array element that is less than the value of $_SESSION['compare']. Using the above values, if $_SESSION['compare'] == 21, then I would love to echo/print "thresholdB". What's the best method to accomplish this? Array_Walk? A switch statement? I first tried while() and was trying to use the pointer in the array, but I found that if I used next() to see if the next array element was larger, the actual use of next() within the while() statement caused the pointer to advance anyways. That seemed like the wrong path to take. The switch statement I've tried is failing, and I don't know how to use a comparison within an array_walk when I want to break out once the largest value is determined. This seems like such a basic function of array and variables but I'm struggling with this. Any advice would be much appreciated. Here's some of my tests that failed: reset($_SESSION['thresholds']); while( (current($_SESSION['compare']) < $_SESSION['thresholds']) and (key($_SESSION['thresholds']) <> 'thresholdMAX')) next($_SESSION['compare']); That final next() statement advances me one step too far. Should I use this and then backup one step? That might create problems of its own. Next I tried switch: switch ($_SESSION['compare']) { case ($_SESSION['compare'] >= $_SESSION['thresholds']['thresholdMAX']): $output = key($_SESSION['thresholds']['thresholdMAX']); break; case ($_SESSION['compare'] >= $_SESSION['thresholds']['thresholdD']): $output = key($_SESSION['thresholds']['thresholdD']); break; But that wasn't working and seems like the wrong way to go about this. Can anyone point me in the right direction? Thanks so much in advance!
- 3 replies
-
- beginner
- best practices
-
(and 3 more)
Tagged with:
-
i have started to write code in php. i know the basics. just want to get started. pls tell me what kind of tasks should i start with. To write my own programs. thank u all in advances...
-
Hello. I've recently started learning PHP, and this has been irking me since: <?php $myTextbox = $_POST['myTextbox']; if(isset($myTextbox)&&!empty($myTextbox)){ echo 'You Typed: '.$myTextbox; } ?> <html> <head> <title>My 39 Site!</title> </head> <body> <form name = 'myForm' action = 'my39file.php' method = 'post'> <input type = 'text' name = 'myTextbox' value = '<?php echo $myTextbox; ?>'/> <input type = 'submit' name = 'submit'/></br></br> </form> </body> </html> It's a simple code I wrote. It has no errors, and it works just fine. The problem is that I don't understand WHY it works. I've been told numerously that the compiler reads code from top the bottom, but I'm failing to see how the rule applies in this case. For instance, at the beginning of the code, I declared the variable $myTextbox and set it equal to the value of the textbox I created in HTML below. This boggles me. The textbox is created AFTER I declare the variable, so how does the computer know what form element I'm talking about? I'm probably just omitting something. Could anybody enlighten me? Thanks.
-
I've just started learning PHP from youtube videos and I'm looking for a friend who's willing to learn so that we can share resources. I think this forum will help me.