PHP - Moved: Read Data From Array
This topic has been moved to Third Party PHP Scripts.
http://www.phpfreaks.com/forums/index.php?topic=352249.0 Similar Tutorials<?php ini_set('display_errors', 0); function escapeArray($array) { foreach ($array as $key => $val) { if(is_array($val)){ $array[$key]=escapeArray($val); } else{ $array[$key]=addslashes($val); } } return $array; } $request_type=$_SERVER['REQUEST_METHOD']; $api_key=$_SERVER['HTTP_X_API_KEY']; $res=array(); if($api_key!=="643256432"){ $res['msg']="Failu Invalid API KEY"; echo json_encode($res); die; } // Connects to the orcl service (i.e. database) on the "localhost" machine //$conn = oci_connect('SCOTT', 'admin123', 'localhost/orcl'); $conn = oci_connect('test', 'test', '192.168.10.43/test.test.com'); if (!$conn) { $e = oci_error(); trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR); } $request=file_get_contents("php://input"); $request=escapeArray(json_decode($request,true)); print_r($request); // die; if($request_type=="POST"){//for creation of invoice echo $CONTACT_ID=isset($request['CONTACT_ID'])?$request['CONTACT_ID']:""; $INV_SERIAL_NO=isset($request['INV_SERIAL_NO'])?$request['INV_SERIAL_NO']:""; $NAME=isset($request['NAME'])?$request['NAME']:""; $INV_DATE=isset($request['INV_DATE'])?$request['INV_DATE']:""; $DUE_DATE=isset($request['DUE_DATE'])?$request['DUE_DATE']:""; $CURRENCY=isset($request['CURRENCY'])?$request['CURRENCY']:""; $SUBTOTAL=isset($request['SUBTOTAL'])?$request['SUBTOTAL']:""; $TAX_TOTAL=isset($request['TAX_TOTAL'])?$request['TAX_TOTAL']:""; echo $SHIP_SERIAL_NO=isset($request['SHIP_SERIAL_NO'])?$request['SHIP_SERIAL_NO']:""; $MASTER_NO=isset($request['MASTER_NO'])?$request['MASTER_NO']:""; $HOUSE_NO=isset($request['HOUSE_NO'])?$request['HOUSE_NO']:""; $shipment_data=isset($request['shipment_data'])?$request['shipment_data']:""; if($CONTACT_ID==""){ $res['msg']="CONTACT_ID is required"; } else if($INV_SERIAL_NO==""){ $res['msg']="INV_SERIAL_NO is required"; } else if($NAME==""){ $res['msg']="NAME is required"; } else if($INV_DATE==""){ $res['msg']="INV_DATE is required"; } else if($DUE_DATE==""){ $res['msg']="DUE_DATE is required"; } else if($CURRENCY==""){ $res['msg']="CURRENCY is required"; } else if($SUBTOTAL==""){ $res['msg']="SUBTOTAL is required"; } else if($TAX_TOTAL==""){ $res['msg']="TAX_TOTAL is required"; } else if($MASTER_NO==""){ $res['msg']="MASTER_NO is required"; } else if($HOUSE_NO==""){ $res['msg']="HOUSE_NO is required"; } else if($SHIP_SERIAL_NO==""){ $res['msg']="SHIP_SERIAL_NO is required"; } else{ $stid = oci_parse($conn, "Select * from FL_HDR_INVOICE where CONTACT_ID='$CONTACT_ID'"); (oci_execute($stid)); oci_fetch_all($stid, $out); if(count($out['CONTACT_ID'])==0){ $stid = oci_parse($conn, "Insert into FL_HDR_INVOICE (CONTACT_ID,INV_SERIAL_NO,NAME,INV_DATE,DUE_DATE,CURRENCY,SUBTOTAL,TAX_TOTAL) Values ('$CONTACT_ID','$INV_SERIAL_NO','$NAME',TO_DATE('$INV_DATE','YYYY-MM-DD'),TO_DATE('$DUE_DATE','YYYY-MM-DD'),'$CURRENCY','$SUBTOTAL','$TAX_TOTAL')"); oci_execute($stid); $stid_2 = oci_parse($conn, "Insert into FL_SHIPMENT_DATA (CONTACT_ID,INV_SERIAL_NO,SHIP_SERIAL_NO,MASTER_NO,HOUSE_NO) Values ('$CONTACT_ID','$INV_SERIAL_NO','$SHIP_SERIAL_NO','$MASTER_NO','$HOUSE_NO')"); oci_execute($stid_2); if(oci_num_rows($stid)>0){ $res['msg']="Invoice created successfully against this contact_id:".$CONTACT_ID; } else{ $res['msg']="Something going wrong please try again later"; } } else{ $res['msg']="contact_id must be unique"; } } echo json_encode($res); die; } I need to read json data inside an array. Please help correct my code.. i am trying to do an API. Hi! I am having trouble reading a stream of data being sent to my PC via PHP5. It seems that my code stops when it gets to the read section and just sits there indefinately. I am somewhat new to PHP so a solution and explanation would be great! Code: [Select] <?php $host = "129.000.00.01"; // host IP address $port = 40000; // port to listen to // sets script execution limit set_time_limit(30); // 0 means unlimited execution time (constant running) // create UDP socket $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP) or die("Could not create UDP socket!\n"); // reset socket for binding if(!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)) { echo "Failed to reset socket!\n"; } else echo "Reset socket successfully!\n"; // bind socket socket_bind($socket, $host, $port); // connect if (!socket_connect($socket, $host, $port)) { echo "Failed to connect to port!\n"; } else echo "Connected to port successfully!\n"; while (1) { echo socket_read($socket, 1024, PHP_NORMAL_READ)."\n"; } // close sockets socket_close($socket); echo "\nDone"; hi, i have the class here, and am trying to save the read column of the textfile to db. but unfortunately it's not being save, i don't know what's wrong... I also attached the textfile so you'll have an idea what am i trying to read from class Extractor { public $filehandler; public $data = array(); // set your db access details private $host = HOST; private $username = USERNAME; private $password = PASSWORD; private $dbname = DBNAME; public function __construct($file) { $this->filehandler = $file; } //prints col1 of textfile public function printcol1() { $fp = fopen($this->filehandler,'r'); while(($this->data = fgetcsv($fp,1000,",")) !== FALSE) { # $this->data[0]."<br />"; $this->savetodb($this->data[0]); } fclose($fp); } public function savetodb($data) { try{ $pdo = new PDO("mysql:host=$this->host;dbname=$this->dbname",$this->username,$this->password); $pdo->exec("INSERT INTO test(col,id) VALUES ({$data},null)"); $pdo = null; }catch(PDOException $e){ echo $e->getMessage()."<br />"; } } } $file = 'iso3166.txt'; $shit = new Extractor($file); $shit->printcol1(); I need to search through a text file and output certain text. My current script doesn't work well at all. Code: [Select] <? $file = 'flist.txt'; $handle = @fopen($file, "r"); $names = array(); $func = array(); $desc = array(); $nme; $fnc; $dsc; $s = -1; $e = -1; $n = 0; if ($handle) { while (($buffer = fgets($handle, 4096)) !== false) { if($nme == 0) { if(strpos($buffer,"<a name") !== false) { $nme = 1; $s = strpos($buffer,"<a name"); } } else if($nme == 1) { if(strpos($buffer,"</span>") !== false) { $nme = 2; $e = strpos($buffer,"</span>"); $num = $e - $s; $names[$n] = strip_tags(substr($buffer,$s,$num)); } } else if($nme == 2) { if($fnc == 0) { if(strpos($buffer,"<table style") !== false) { $fnc = 1; $s = strpos($buffer,"<table style"); } } else if($fnc == 1) { if(strpos($buffer,"</td>") !== false) { $fnc = 2; $e = strpos($buffer,"</td>"); $num = $e - $s; $func[$n] = strip_tags(substr($buffer,$s,$num)); } } else if($fnc == 2) { if($dsc == 0) { if(strpos($buffer,"<p>") !== false) { $dsc = 1; $s = strpos($buffer,"<p>"); } } else if($dsc == 1) { if(strpos($buffer,"</p>") !== false) { $fnc = 0; $nme = 0; $dsc = 0; $e = strpos($buffer,"</p>"); $num = $e - $s; $desc[$n] = strip_tags(substr($buffer,$s,$num)); } } } } } if (!feof($handle)) { echo "Error: unexpected fgets() fail\n"; } fclose($handle); $n = 0; while($n < 100) { echo $names[$n] . "<br>"; echo $func[$n] . "<br>"; echo $desc[$n] . "<p>"; $n ++; } } ?> 4096 gets the data for each line, right? Well some of my data uses two lines. After all the searching is done I need it to output $names, $func, and $desc in plain text. Hey guys, The script I use to generate images, more or less, works almost flawlessly. However, I keep experiencing a problem at random in my script when it comes time to call a JPEG or PNG file from an external server. A lot of the time it will work fine, but many other times it comes back with the error: imagecreatefromjpeg() [function.imagecreatefromjpeg]: Cannot read image data Which causes the script to fail. Right now the images it say it cannot read are these: http://tiles.xbox.com/tiles/UT/EF/1mdsb2JgbA9ECgQLGwMfWSkgL2ljb24vMC84MDAwIAABAAAAAPkqMU4=.jpg http://tiles.xbox.com/tiles/Au/lM/1Wdsb2JgbA9ECgUAGwEfL1hTL2ljb24vMC84MDAwIAABAAAAAPpj6R0=.jpg http://tiles.xbox.com/tiles/6q/kv/1Gdsb2JgbA9ECgUAGwEfL1hSL2ljb24vMC84MDAwIAABAAAAAPsAqfU=.jpg http://tiles.xbox.com/tiles/tQ/UG/1Gdsb2JgbA9ECgUAGwEfV1gmL2ljb24vMC84MDAwIAABAAAAAPspBao=.jpg http://tiles.xbox.com/tiles/qp/Fx/0Wdsb2JgbA9ECgQNGwEfVitXL2ljb24vMC84MDAwIAABAAAAAP5ekbU=.jpg and as you can see, they work fine. So what's going on here? Is Microsoft somehow blocking the attempt? I can view the images fine in the browser, but sometimes it just won't work in the script. But like I said, it's not everytime. The line the errors comes up on are these: $lastxboxgames = imagecreatefromjpeg($lastxboxgames); $lastxboxgames1 = imagecreatefromjpeg($lastxboxgames1); $lastxboxgames2 = imagecreatefromjpeg($lastxboxgames2); $lastxboxgames3 = imagecreatefromjpeg($lastxboxgames3); $lastxboxgames4 = imagecreatefromjpeg($lastxboxgames4); I also have the script to echo the variables back to me when while it's running, so those urls up there came exactly from the script, so it's not that its getting the wrong URL, it just decides that the image isn't good enough. The script, generates my signature image below, and when any of the images that come from the xbox server return an error, they all do, including the avatar image in the top left which is a PNG. Any help is appreciated! I'm trying to send some POST data to a page and then get the contents of it. The page is not on my server, so I can't use Sessions or GET. Google hasn't provided me with a definite answer. How would I do this? After searching postings on multiple forums, I am officially now "Freaking OUT" trying to understand something that is probably very simple, but cannot seem to grasp. I simply want to read basic .csv data that is sent/uploaded directly to a PHP page, and to then append each record that shows up to a single .csv file on the server. The incoming data is supposed incoming via $_POST['csv'] ... at least that's what I was told. Each.csv record line being sent/uploaded is very simple (either single or multiple records in one small file): text1,text2,text3,text4,text5 For additional processing I know about 'explode', etc., but right now I am stuck even trying to do an 'echo' to display the simple incoming data "as is". One option I tried was: $postdata = file_get_contents("php://input"); echo $postdata; In the Java App monitoring I get the following after 3 records are sent/uploaded to the PHP URL: Server response status line: HTTP/1.1 200 OK I find this strange since I did not include... http_response_code(200); ... in the page code. Obviously, I do NOT know what I am doing here {SIGH}. Any assistance or guidance is appreciated. Thank you ! - FreakingOUT
Hi, I am trying to fetch a row in advance of the current one to find out if the next result is the same or not, and do something based on the outcome. I know how to do this with the old mysql functions like mysql_data_seek. What is the equivalent in PDO? I want the below sample to return row number 2, but it returns nothing no matter what number I set the offset to be. $readAhead = $resultParts->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_REL, 1) ; echo $readAhead['partNumber'] ; array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL) is set in my prepare statement. hi guys, i want to retrieve data from database table using select query store the result into an array in database class and then in view class e.g in html i want to make an object of that database class and call the array and display the data into html table. someone help would be appreciated. Thanks in advance. $handle = fopen("/etc/rpt.fifo", "r") or die("Unable to open file!"); $buffer = trim(fgets($handle)); $ary = explode (",", $buffer); $x = $ary[2]; print $x; flush(); pclose($handle); When I run it, it reports: Notice: Undefined offset: 2 on line 4 This is the line in /etc/rpt.fifo that is being read (there are 4 indices) 20210721153256,1100,TXKEY,89790 The error makes no sense to me but I'm no expert <g> Edited July 21 by KenHorseThis topic has been moved to Other Libraries and Frameworks. http://www.phpfreaks.com/forums/index.php?topic=307986.0 Hi there... This array thing seems little strange and I had to turn to phpfreaks I have an array named: $optionInfo When we do: print_r($optionInfo); it displays all the values stored in the array i.e. Array ( [4] => Array ( [optionId] => 5 [optionName] => Blue - Medium [optionListId] => 4 [dateCreated] => [enabled] => 1 ) ) And as we can clearly see, we have the optionId value as 5. So therefore, when I type: Code: [Select] print_r($optionInfo[optionId]); It returns nothing. Even though there is a value 5 in the array but I just can't get the individual value of optionId. Is there something I am missing here. Kindly reply. Thank you! Cheers! I'm creating a tool for building project proposals for clients, with pricing. I have a database with two tables: services and clients. The services table looks like this: Code: [Select] | code | name | cost | +-------------+-------------+------+ | logo_design | Logo design | 10 | | web_design | Web design | 20 | The clients table looks like this: Code: [Select] | id | client | logo_design | web_design | +----+---------+-------------+------------+ | 1 | Walrus | yes | yes | | 2 | Narwhal | no | yes | How would I link the results from these two tables so that I only pull out the services each person wants from the database? Here is what I have so far: Code: [Select] <? $sql = "SELECT * FROM services"; $result = mysql_query($sql); while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) { echo $row{'name'} . ': $' . $row{'cost'} . '<br />'; } ?> This, of course, displays all the services, with all the prices, like so: Logo design: $10 Web design: $20 I want to add something here so that based on the selected client, we see only the services/prices they selected (ol' Narwhal's page would not show the "Logo Design" line). I've got the data from "clients" pulled in to this page as well, but don't know how to relate it to "services." Thanks in advance! Let me know if I've left out any info that would help you to point me in the right direction. This topic has been moved to Microsoft SQL - MSSQL. http://www.phpfreaks.com/forums/index.php?topic=319147.0 Hello together, I'm still relatively new with PHP and I'm encountering an error with the error code (Warning: Undefined array key "title" in I:\xampp\htdocs\test\Datenspeichern.php on line 13 to 17.). Normally I wanted that when I enter the data in the fields from the CD_Website page, that it is inserted in my created database and displayed in the table on the page. Does anyone know where my error is? Thanks a lot
main Code= <!doctype html> <html> <head> <meta charset="utf-8"> <title>Unbenanntes Dokument</title> <style type="text/css"> table { border-collaps: collapse; width: 100%; font-family: serif; font-size: 35px; text-align: center; } td { font-size: 25px; text-align: center; font-family: serif; } </style> </head> <body> <table> <tr> <th>CD-Titel</th> <th>Artist</th> <th>Songtitel</th> <th>Musiklänge</th> <th>Lied Nummer</th> </tr> <?php $host = "localhost"; $user = "root"; $password = ""; $db_name = "cdaufgabe"; $con = mysqli_connect($host, $user, $password, $db_name); if(mysqli_connect_error()) { die("Verbindungsabbruch mit der Datenbank: ". mysqli_connect_error()); }; $check = "SELECT * FROM `cd-titel`"; $result = mysqli_query($con, $check); if ($result > null) { while ($row = $result->fetch_assoc()){ echo "<tr><td>" . $row['CD-Titel'] . "</td> <td>" . $row['Artist'] . "</td> <td>" . $row['Songtitel'] . "</td> <td>" . $row['Musiklänge'] . "</td> <td>" . $row['Lied-Nummer'] . "</td></tr>"; }; }; /*if (isset($_POST['submitted'])) { $titel = $_POST['titel']; $artist = $_POST['artist']; $songtitel = $_POST['songtitel']; $musicle = $_POST['musicle']; $liednr = $_POST['liednr']; $data_add = "INSERT INTO cd-titel (CD-Titel, Artist, Songtitel, Musiklänge, Lied-Nummer) VALUES ('$titel', '$artist', '$songtitel', '$musicle', '$liednr')"; if (!mysqli_query($db_name, $data_add)) { die('Fehler beim einfügen von Daten'); } } */ // $data_add = "INSERT INTO `cd-titel` (`CD-Titel`, `Artist`, `Songtitel`, `Musiklänge`, `Lied-Nummer`) VALUES ('$titel', '$artist', '$songtitel', '$musicle', '$liednr')"; // mysqli_query($con, $data_add); /*mysqli_query($con, $data_add);*/ ?> <form methode="post" action="Datenspeichern.php"> <input type="text" name="titel" placeholder="CD-Titel"/> <input type="text" name="artist" placeholder="Artist"/> <input type="text" name="songtitel" placeholder="Songtitel"/> <input type="text" name="musicle" placeholder="Musiklänge"/> <input type="text" name="liednr" placeholder="Lied-Nummer"/> <input type="submit" name="submitted" value="speichern"/> </form> </br></br></br> </table> </body> </html> second code(Datenspeichern.php)= <?php $con = mysqli_connect('localhost', 'root', ''); if (!$con) { echo'Nicht verfügbar'; } if (!mysqli_select_db($con, 'cdaufgabe')){ echo 'Datenbank nicht ausgewählt'; }; $titel = $_POST['titel']; $artist = $_POST['artist']; $songtitel = $_POST['songtitel']; $musicle = $_POST['musicle']; $liednr = $_POST['liednr']; $data_add = "INSERT INTO cd-titel (CD-Titel, Artist, Songtitel, Musiklänge, Lied-Nummer) VALUES ('$titel', '$artist', '$songtitel', '$musicle', '$liednr')"; if (!mysqli_query($con, $data_add)){ echo 'Fehler'; } else { echo'Eingefügt'; }; header("url=CD_Webseite.php"); ?> Does anyone knows the mistake i keep doing? I want to create a singleton class that read multidimensional array from txt file and flatten the array retrieved. This topic has been moved to Third Party PHP Scripts. http://www.phpfreaks.com/forums/index.php?topic=319414.0 Hi, after banging my head against the wall for a while thinking this would be a simple task, I'm discovering that this is more complicated than I thought. Basically what I have is a link table linking together source_id and subject_id. For each subject there are multiple sources associated with each. I had created a basic listing of sources by subject... no problem. I now need a way of having a form to create an ordered list in a user-specified way. In other words, I can currently order by id or alphabetically (subject name lives on a different table), but I need the option of choosing the order in which they display. I added another row to this table called order_by. No problem again, and I can manage all of this in the database, however I want to create a basic form where I can view sources by subject and then enter a number that I can use for sorting. I started off looping through each of the entries and the database (with a where), and creating a foreach like so (with the subject_id being grabbed via GET from the URL on a previous script) Code: [Select] while($row = mysqli_fetch_array($rs)) { //update row order if (isset($_POST['submit'])) { //get variables, and assign order $subject_id = $_GET['subject_id']; $order_by = $_POST['order_by']; $source_id = $row['source_id']; //echo 'Order by entered as ' . $order_by . '<br />'; foreach ($_POST['order_by'] as $order_by) { $qorder = "UPDATE source_subject set order_by = '$order_by' WHERE source_id = '$source_id' AND subject_id = '$subject_id'"; mysqli_query($dbc, $qorder) or die ('could not insert order'); // echo $subject_id . ', ' . $order_by . ', ' . $source_id; // echo '<br />'; } } else { $subject_id = $_GET['subject_id']; $order_by = $row['order_by']; $source_id = $row['source_id']; } And have the line in the form like so: Code: [Select] echo '<input type="text" id="order_by" name="order_by[]" size="1" value="'. $order_by .'"/> (yes I know I didn't escape the input field... it's all stored in an htaccess protected directory; I will clean it up later once I get it to work) This, of course, results in every source_id getting the same "order_by" no matter what I put into each field. I'm thinking that I need to do some sort of foreach where I go through foreach source_id and have it update the "order_by" field for each one, but I must admit I'm not sure how to go about this (the flaws of being self-taught I suppose; I don't have anyone to go to on this). I'm hoping someone here can help? Thanks a ton in advance hi i am trying to make a payroll calculator script that takes employee info, calculates pay, displays submitted info in a table, stores info in an array, and updates the array when new info is submitted. i have most of these accomplished, i am having trouble with the "store into an array, and update the array when new info is submitted" parts of the project. i am still not very fluent in php so there may be easier ways to achieve what i have so far. any pointers would be a great help, this part has got me stumped. Using curl_multi, I have loaded up 3 url's and then printed the array. 1) How can I also set a timestamp on output to let me know when each url was run? 2) How can I explode the array to only display the data and timestamp, excluding the text "Array ( =>", "[1] =>", "[2] =>" and " )"? Code <?php function multiRequest($data, $options = array()) { // array of curl handles $curly = array(); // data to be returned $result = array(); // multi handle $mh = curl_multi_init(); // loop through $data and create curl handles // then add them to the multi-handle foreach ($data as $id => $d) { $curly[$id] = curl_init(); $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d; curl_setopt($curly[$id], CURLOPT_URL, $url); curl_setopt($curly[$id], CURLOPT_HEADER, 0); curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1); // post? if (is_array($d)) { if (!empty($d['post'])) { curl_setopt($curly[$id], CURLOPT_POST, 1); curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']); } } // extra options? if (!empty($options)) { curl_setopt_array($curly[$id], $options); } curl_multi_add_handle($mh, $curly[$id]); } // execute the handles $running = null; do { curl_multi_exec($mh, $running); } while($running > 0); // get content and remove handles foreach($curly as $id => $c) { $result[$id] = curl_multi_getcontent($c); curl_multi_remove_handle($mh, $c); } // all done curl_multi_close($mh); return $result; } $data = array(array(),array()); $data[0]['url'] = 'http://search.yahooapis.com/VideoSearchService/V1/videoSearch?appid=YahooDemo&query=Pearl+Jam&output=json'; $data[1]['url'] = 'http://search.yahooapis.com/VideoSearchService/V1/videoSearch?appid=YahooDemo&query=Black+Eyed+Peas&output=json'; $data[2]['url'] = 'http://search.yahooapis.com/VideoSearchService/V1/videoSearch?appid=YahooDemo&query=Nirvana&output=json'; $r = multiRequest($data); print_r($r); ?> Output Array ( => Pearl Jam [1] => Black Eyed Peas [2] => Nirvana ) Preferred Output 01:00:01 Pearl Jam 01:00:02 Black Eyed Peas 01:00:03 Nirvana |