ODBC: Fetch Results As Associative Array
Will PHP fetch the results of a query using ODBC as an associative array? I know that MySQL does that, but I couldn't find it for ODBC connections.
View Complete Forum Thread with Replies
See Related Forum Messages: Follow the Links Below to View Complete Thread
Function To Fetch Database Results
I have a function that is supposed to fetch all the results from my table "games". There are two database records in that table. My function is supposed to display all of the records, but it only displays one. I also use a function to display a certain template. Here is the coding for both functions and my index. Code:
ODBC Query Results
Consider the following: <? $username = "foo"; $password = "bar"; $host = "db"; $connect = odbc_connect("$host", "$username", "$password") or die; $query = "SELECT id, name, address, city, state, comments1, comments2 FROM data.table1, data.table2 WHERE id = ô˜'"; $result = odbc_exec($connect, $query); while(odbc_fetch_row($result)){ $id = odbc_result($result, 1); $name = odbc_result($result, 2); $address = odbc_result($result, 3); $city = odbc_result($result, 4); $state = odbc_result($result, 5); $zip = odbc_result($result, 6); $comments1 = odbc_result($result, 7); $comments2 = odbc_result($result, 8); print("ID: $id<br>Name: $name<br>Address: $address<br>City: $city<br>State: $state<br>Zip: $zip<br>Comments1: $comments1<br>Comments2: $comments2<br> "); } ?> Only one record is returned from data.table1 (containing the id, name, address, city, state, & zip). However, there are multiple records returned from data.table2 (records from comments1 & comments2). What is happening is that when I run this script, all of the data returned from data.table1 is printed over & over again along with each record returned from data.table2. How can I make it such that id, name, address, city, state & zip are only printed once - and allow comments1 & comments2 to be printed multiple times for each occurance in the database? I've thought about just creating a second database query for the records in data.table2, however I need the join I'm getting by using the existing query. For what it's worth, I'm connecting to a DB2 database using PHP / Apache on Linux. I'm confused by this.
ODBC Results Sorting And Adding?
I need to go through ODBC results and add Quantity for like items. Quick example: Database Is: ItemQntyDate foo11-10-99 bar51-15-00 foo21-11-99 I need to somehow search through results and add matching Item Quantities together. In this example, the return I would expect is: ItemQntyDate bar51-15-00 foo31-10-99/1-11-99 because the foo Qnty added together became 3. Any thoughts on how to accomplish this? Load the ODBC results into an array and somehow sort through the array? Never done that before...
Using The $value Of An Associative Array
I am using an associative array to populate one field of a form. I use the $key to obtain the numeric value I want to enter into one of my tables. I use the $value to display in nice text what the numeric key represents. I have no problem entering the $key into the table or displaying the $value on the form. My problem is in displaying the results of the processed form to the user: My form "action" is "processform". In processform I do the INSERT query and return a message to the user. It is in the message to the user where my problem comes in. I've only captured the $key from the form. How can I capture also the $value from the form post, so that when I display to the user what he or she has chosen on the form, the nice text will display rather than the (to them) meaningless id number? Code:
Associative Array
I have to search a db table, and count distinct records to get some results from a survey we've done. The record needs to be echoed along with the number of times it is recorded. Each field in the table has to be searched individually, so that we can see the answers for each question separately. I thought that I could do this with one query, maybe 'count distinct', but I couldn't get it to show both the value of the row, and the amount of times that value had been recorded. So, I have to do 2 queries, one getting the distinct values of the field, the other counting how many times they are recorded. Here's the 2 queries: Code:
Associative Array
I have a list of word/number pairs. I need to retrieve the word that has the highest number associated with it. I have put this list into an associative array... $array['apple'] = ཈' $array['orange'] = Ɖ' $array['pear'] = ཮' ...and ordered it using arsort() so that "pear" in this case is the first value, then hit a brick wall when I realised that echoing $array[0] didn't work on an associative array. Indeed, why would it? :) Is there a simple way to retrieve the top value after it's sorted, or do you always need to know the name of the key to manipulate associative arrays? Else, do I need to look into multidimensional arrays? Something like... $array[0] = array('apple' => ཈'); $array[1] = array('orange' => Ɖ'); $array[2] = array('pear' => ཮'); At which point, I'm not clear on how this would be sorted based on the values of the nested arrays, and how you would then specify to retrieve the key of the nested array of the first key of the original array!
Associative Array In Class
In PHP Version 4.3.6 I am trying get a value from a class member that is an associative array... class MyClass { $var row; function MyClass() { $this->$row = array("A"=>"Apple", "B"=>"Bob"); echo "One: " . $this->$row["A"] . "<br>"; echo "Two: " . $this->row["A"] . "<br>"; $cache = $this->$row; echo "Three: " . $cache ["A"] . "<br>"; } } But this is the output... One: Array Two: Three: Apple How can I get at the value of the associative array without making a local copy first!
Array_push() With An Associative Array?
Is there a way to add a key/value pair to an associative array? I'm tryint to do this, but it dosn't work. $author = array(); while ($row = mysql_fetch_array($result)){ array_push($author,$row[id],$row[name]);}
If Fetch Array Is Empty ?
I'm doing a fetch_array and I would like to display a "no records found" message to screen. For some reason this is not working: here's my Postgres SQL as proof: surveys=# select othermore from table where othermore <> '' othermore ----------- (0 rows) Then I have: $result = pg_query($conn, "select othermore from table where othermore <> ''"); while ($row = pg_fetch_array($result)) { if (!$result) { echo "no records<br> "; } else { echo "$row[0]<br>"; } } Strnage ? Can someone shed some light on what UI am doing wrong ?
Associative Array Question - Newbie
I am in the process of designing (before coding) my first serious PHP/MySQL application and I have a question about the best way to store information. The application will allow users to view the voting record of different legislators. I have concluded that the most efficient way to store the data is in two tables: one that has details about each legislator (mp) and another that has the details of each piece of legislation (bill). Each legislators record will have a field that holds info in an associative array in the form (bill ID, vote result). This way, when a new piece of legislation is added to the database, one more value pair (bill ID, vote result) will be added to each legislators voting record field. My question is two part: Is this the most efficient way to store the data? and I suspect I will have to be using regular expressions to extract and format the data from the MySQL query. Are there some PHP or MySQL functions or syntax I would find useful in this process of retrieving the data? Any online tutorials? (mysql_fetch_array appears to create an array out of two fields - not really what I am looking for...) I realize this is a rather vague question, but if you have any suggestions, please let me know.
Make Associative Array Out Of String
I have a string that look something like this: $string = "weight, kg|1200|width, mm|220|prize|20000"; and I would like it to an associative array and print keys and value: weight, kg: 1200 width, mm: 220 prize: 20000 (The string above is fetched from a database) Anyone have any bright ideas how to do it?
Adding Values Onto An Associative Array...
I'm trying to develop a simple class which through the interface you can add elements to an array. The method I have used to add the elements is setContentFields($name, $field_type), I have used an array counter variable to increment the array index each time. To me this seems like a workaround, and I wondered if there was a more elegant way of doing this. The class is as follows:
Sorting An Associative Array Of Objects By Value?
I'm trying to sort an array of objects within an object. Included is a dumbed down example so that we can get at the meat of the issue without worrying about complexity or validation. Basically, Funk is an object that has a name (string) and a value(int). FunkThis represents an object containing a list of Funks, with each key corresponding to the name of the Funk. I want to sort this list by val, but I get a Warning: usort(): Invalid comparison function error when I run the script. Keep in mind that this is for PHP 4 I'm open to suggestions to getting this to work. <?php class Funk { var $name; var $val; function Funk($name,$val) { $this->name = $name; $this->val = $val; } function getVal() { return $this->val; } function getName() { return $this->name; } } class FunkThis { var $list; function FunkThis() { $this->list = null; } function add($funk) { $this->list[$funk->getName()] = $funk; } function compareFunks($a,$b) { $aVal = $a->getVal(); $bVal = $b->getVal(); if ($aVal == $bVal) return 0; if ($aVal $bVal) return 1; if ($aVal < $bVal) return -1; } function sortByFunk() { $list = $this->list; $this->list = uasort($list,"compareFunks"); } function show() { print_r($this->list); } } $a = new Funk("A",3); $b = new Funk("B",7); $c = new Funk("C",1); $d = new Funk("D",9); $e = new Funk("E",6); $funkthat = new FunkThis(); $funkthat->add($a); $funkthat->add($b); $funkthat->add($c); $funkthat->add($d); $funkthat->add($e); $funkthat->sortByFunk(); $funkthat->show(); ?>
[smarty] Addressing Associative Array
Given this assoc array: $dtl_array[0]['uom_array'][0][0]='key1' $dtl_array[0]['uom_array'][0][1]='value1' $dtl_array[0]['uom_array'][1][0]='key2' $dtl_array[0]['uom_array'][1][1]='value2' $dtl_array[1]['uom_array'][0][0]='key3' $dtl_array[1]['uom_array'][0][1]='value3' $dtl_array[1]['uom_array'][1][0]='key4' $dtl_array[1]['uom_array'][1][1]='value4' $sample->assign("dtl_array",$dtl_array); $sample->display(); How could I create a combobox out of $dtl_array in the templates? The following does not work: <select name="aname"> {section name=mm start=0 loop=$dtl_array.0.uom_array step=1} <option value="{$dtl_array.0.uom_array.mm.0}"> {$dtl_array.0.uom_array.mm.1} {/section} </select>
Values From Form Via Associative Array
I have a form which has a text field and a select box. There are many other fields also in the form . I need get the value of the text field or the select box, depending on which one has a value. Additionally it has to give the value only from the text field even if there is a selection made from the select box. I know that i can get post values from form like this: PHP Code:
How To Add An Element To The Middle Of An Associative Array ?
I got an array that consists of elements that are arrays also. Now I wish I could add an element to the middle of it. Let mi give you an example: array ( - array(1,15,apple), - array(2,28,banana), - array(3,41,orange) } I would like to add, let's say, carrot on 2nd position: array ( - array(1,15,apple), - array(2,57,carrot), - array(3,28,banana), - array(4,41,orange) } Array_splice() does not work (or maybe i use it in a wrong way? - it splits an adding element for, in that case, 3 elements). Array_push() adds only to the end of an array. can samobody tell me how to add and element to the middle of an associative array?
Sort An Associative Array Alphabetically
I am working on a product catalog where it displays a product type on a page via the type variable. The products should be displayed alphabetically by $make and $model and i couldn't find any snippets or info other than asort etc. Just curious if anyone else needed such a sort function and how they did it. ie: product makes Maico, Madsen, Bio-logic etc and their models MA-300, Itera, AC-200. So I would have listings like Maico MA-300, Maico MA-800, Madsen Itera etc., and I need these alphabetical using make and model values. PHP Code:
How To Print A Multidimensional Associative Array?
I have the following associative array: $user["john"]["mozilla"]=1; $user["john"]["xmms"]=1; $user["doe"]["mozilla"]=0; $user["doe"]["office"]=1; $user["paul"]["mozilla"]=0; $user["paul"]["xmms"]=1; $user["paul"]["office"]=1; How can I print such an associative array using a loop statement like foreach?
Setting Values In Associative Array To 0
Ive tried almost every combination of while,foreach, key, value , array,0 I can some up with Ive got an associative array that Ive been using as a counter eg if (blah blah blah ) {$array['beans']++} elseif (something else){$array['peas']++} else {$array['carrots']++} I then want to reset all the values to zero foreach ($array as $key=>$value) { ??????? } or array_walk ?
XML Formatted String To Associative Array
I'm working on a php socket server for my chat room, and I need to parse some very short XML messages like this: <msg t="usr"><body a="pubMsg" u="username"><![CDATA[some message]]></body></msg> Right now I'm using a bunch of substrings (start laughing. lol), but it's very inflexible. However somehow it works... The problem arises when I try to store user variables (ignore the indents and line brakes, in reality, there will be non of them; we don't want to waste bandwidth) <msg t="usr"> <body a="updVar" u="username"> <var t="s" n="varname"><![CDATA[some variable]]></var> <var t="n" n="someNum"><![CDATA[3]]></var> </body> </msg> I have no idea how to parse those stuff from a string to an associative array... What I want is an associative array that I can access those stuff similar to this way: echo myXML['firstChild']['firstChild']['firstChild']['attributes']['t']; and it should output "s"
PHP Stops Processing On Multidimensional Associative Array
When using a multidimensional associative array to cross-reference data in a second, single-dimensional array, PHP stops processing data after a few iterations. Memory usage according to the task manager doesn't seem to spike and CPU usage only gets as high as 11 to 13 percent. A sample script is provided below, any help or alternative solutions are welcome. (By the way, the script below works fine on Linux with the same version of Apache and PHP. Possibly a PHP configuration issue?)
Printing Out An Associative Array - Whats Wrong?
I have this code segment: $query = "Select * from Payments order by CustID"; .. .. /* execute the query */ .. if (ora_exec($cursor)) { $recordset=array(); while (ora_fetch_into($cursor,$recordset,ORA_FETCHINTO_N ULLS| ORA_FETCHINTO_ASSOC)) { echo $recordset["CustID"]."<BR>"; echo $recordset["Amount"]."<BR>"; } } Gives me the following error Notice: Undefined index: CustID Notice: Undefined index: Amount The table Payments has only CustID and Amount as attributes. is my usage of the array offset incorrect?
Complex Associative Array To XML FIle And Back
We're using php 4.1 and it doesn't seem to have built in support for this. Coming from a dotnet background this surprised me...Anyways, thats a different topic altogether... I'm looking for a php class that can allow me to save a complex associative array as an xml file on our server and also be able to read in back into a complex array when needed. (you'll find an example of the array below). Because of the complexity if the array, I feel it would be easier to use this method as opposed to saving to a db. I know i could write a class to do it, but I'm trying not to reinvent the wheel. Googleing only refers me to extentions like pear which I dont want. And I 'm sure someone else has had to do this before. //ARRAY EXAMPLE ...
POST (or GET) Associative Array To Script From Form
Actually thought I understood this but I can't seem to make it happen this morning, and I can't seem to find a clear simple reference here. Can I pass an associative array from a form (using either POST or GET) to a .php script all at once, in one simple step? ie. Say I have the following variable/associative array $graphtable = Array ( [TESTPHASE] => G085 [GRADE] => 3 [0_LE_1_A] => 0 [1_LE_1_A] => 7 [2_LE_1_A] => 28 [3_LE_1_A] => 22 [4_LE_1_A] => 7 [null_LE_1_A] => 0 [PROTOCOLS] => 64 ) And I pass it as a hidden variable in the following form: ....
Mysql Fetch Array - Image Display If Available
I am using a HTML search form to give me the following result. I can get everything to work, even get it to display images that are associated with particular search term, but can't figure out how to keep it from displaying that annoying little "There's not image here image" that explorer shows, the one with box and red "x". I know that an if statement would do the trick, but I can't do "if's" inside of the "echo" is it possible to accomplish this? How? Code:
Fill A Array From ODBC Connection
I have a database access to which access through an ODBC connection. I wonder how can I fill in an array with the "Select" launched to the database. Thank you....
Sorting Sql Results In An Array Using Things Outside The Array To Sort Them?
I'm using PHP to connect to a database full of place names, and their associated post codes, latitude and longitude on the earth. A user types his or her post code into a form, plus a distance (in miles), and the script looks for other area codes (first half of the post code) within that distance. I can do that fine, but the results are ordered alphabetically by postcode, and I want to order it by the distance. Problem here, is that the distance isn't part of the array taken from the database, it takes the latitude and longitude of the area it's on in the while loop, and calculates the distance between the user's post code and that one, but it's not part of the array, so I can't sort the array using it. How can I sort it? Can I add the calculated distances to the array, and sort it using sort()? If yes, how do I add them to the array? Here's the page, just to explain better what the script does: http://navimaker.org/ed/owain/results.php?postcode=gl15&distance=34 Mess around with the postcode and distance vars. I'd put the source on here too but I'm doing all this over SSH and can't copy & paste very easily .
Looping Through An Array And Using The Results.
function build_menu() { $all_li = create_list_items(); $menu = '<ul id="navbar">' reset($all_li); while($li = each($all_li)) { $menu .= $li; } $menu .= '</ul>' echo($menu); } I'm trying to move through each item in the #all_li array and add it's contents to the $menu variable. $all_links is filled with string values from the function create_list_items(). I tried (above) using each(), but the output from the function is "ArrayArrayArray" and this has me confused. I think it's giving me this because each() creates an array which includes key values (yes?), which means each() is not the method I need to use. Can anybody advise me on a method I can use to loop through the $all_links array and add each string to the $menu variable? So far I've tried a for loop, foreach, even attempted an array_walk(), but haven't had any success.
Populating Array With Mysql Results
I'm returning a result set of one field in a table, and want to populate an array with the results. I can't for the life of me figure out a simple way to do this without using mysql_fetch_array() to cycle through the results, append that to a var, then explode that into a var and pass that. PHP Code:
Displaying Array Results In Table
I'd like to display the results of an array in a table format with the Alias, Alias Destination & Username as column headings & the results underneath. PHP Code:
Store Mysql Results Into An Array
i just started using php. i've used it before as a maintainer. fun stuff. well now i'm the writer. i want to abstract all the DB commands away from myself and all other developers. so i'm writing a function to query the php database from all users of the database and store them into an array as a return value so that i can seperate my database code from my html creation code. 1. is there a way to put all my database functions into a php file and include that file into all my other php pages? similiarly to a #include in C++ or a import package name in Java? 2. my code to run down the fetched results and store them into an array doesn't seem to be working the way i want it to. if i print out the results from the mysql returned fetch the data is fine. however the array seems to print out "Array[elementnumber]" instead of the value. Code:
Evaluating Array And Skipping Certain Results
I'm having a brain freeze on this. I have a script where I am reading the contents of a file into an array. I have that up and working with no problem. Then I'm exploding the array using while ($line = fgets($fd, 4096)) { $arrayData = explode("-", trim($line)); } Works fine. But what I want to do now is evaluate arrayData[2] to see if it contains the integers "00" (that's zero zero) and if it does, I want it to skip that entry. I know I need to set it up with if arrayData[2] == "00" { But I'm drawing a blank on how to get it to skip this entire line in the final results. There may be multiple instances of this happening in each file and I want to skip them all.
How Do I Search Two-dimiensional Results Set Array
Here's the pertinent code to get the SQL results. It is generated by dreamweaver adn I'm trying to learn to code php so I don't depend on DW to do it for me. $query_rsSummary = "SELECT sid, Count(wid) FROM plotting GROUP BY sid"; $rsSummary = mysql_query($query_rsSummary, $tableG) or die(mysql_error()); $row_rsSummary = mysql_fetch_assoc($rsSummary); How do I search the array to see if a certain "sid" has an entry in the array?
Building Array Of Difft Results For Return
I have 2 functions that I want to call from a subroutine, and then return their results; one result is an array, the other will be an array of arrays. The first will be the information about a product, $product, with values like $product['title'], product['id'], etc. The second will be an array of arrays, each line of which contains information about an element of the product. I.e., $item[0]['title'], $item[2]['value'], etc. How would I best combine these so I can RETURN the total thing from the function, and then how would I refer to the parts in the calling code? For example, if I said Code:
Taking SELECT Results And Turning Them Into An Array()
Does anyone know how take a mysql select statement and have the results placed into an array? I am returning 1+ rows and want to place those rows into an array so I can run a foreach() for each row and then run another foreach() for each column for each row? Code:
How To Make An Array With Query Results? (Postgresql)
Need help making an array with query results: $value = $_POST['text']; $pg = pg_connect('dbname=test user=user password=secret'); $query = "SELECT column FROM information"; $result = pg_query($query); *** how to create array with $results ***
Query Array, Sort Results And Display In Seperate Tables
The problem I have it breaking out the results and displaying them (broken out) alphabetically in seperate tables. For instance, I have generated (bookmarked) tables, one for each letter (A-Z) and want to display all recordsets with say, last name beginning with "A" in one 2 column table, "B" in another 2 column table, and so on... each table would have two columns (with 2 background colors alternating), each cell containing values of one recordset. Even more tricky is if I have an odd number of cells, still generating the last cell with empty values (for asthetics). And can anyone say "sort" (yes, I am nuts)? The ability to sort the whole mess by any column value. I have looked at freeware and other third part apps, but they can only do what I already can do (mine is actually cleaner) but not as complicated.
Array Results Output On Online As Opposed On Separate Lines
How do i get to echo details on oneline as opposed to the next line. $car=array("red", "green", "white", "blue", "orange"); foreach $car as $color) { echo "$color<BR/>"; I dont want the results on separate lines but on the same line with commons...
Pushing Data Into A Multi-dimensional Array From Mysql Results
First, A result array I'd want to get could look like this: $chart['chart_data'] = array ( array ( "Region A", 10,12,11,15,20,22,21,25,31,32,), ); The first is "Region A" and the rest is just int's that I'd pull from the db, so to set it I'd do this: $chart [ 'chart_data' ][ 0 ][ 0 ] = "Region A"; But the problem is when I want to insert the data in the while loop (like the 10,12,11,15...), I've tried array_push but it's not working, maybe it doesn't work with multi-dimensional arrays? Anyways, here.'s what I tried, the following inside the "while" to loop the mysql results with mysql_fetch_assoc: while($r=mysql_fetch_assoc($res)) { $chart [ 'chart_data' ][ 0 ][ 0 ] = array_push($chart['chart_data'][0][0], $r['wght']); // the above line is where I have problems ^^ } What I want is on each loop to insert the fetched data inside the array so that it gives what I gave first at the top, but what I've written above doesn't work, it gives me a warning saying "first argument must be an array" which seems to be one, but anyway, anybody know how to do this?
Fetch?
If I want to retrieve a single row. I would make a query and than use msql_fetch_row. (If I use this in a loop it will fetch the next row until it becomes false). If I want to retrieve multiple rows. I would make a query and than use msql_fetch_array etc.. Say I use a loop with msql_fetch_array and display the data in a table do I have to make a new query to display say the third row of the original table somewhere else on the page?
Associative Arrays
Basically I am writing a shopping cart from scratch. I can get my first item looking good but I can never add to my cart. If I choose another item it seems to overwrite the first one. Code:
Fetch & Process
I doing a straight forward webpage fetch and saving it to a file: CODE: $open = @fopen($url, "r"); $urlfile = @fread($open, 50000); @fclose($open); $page = split("",$urlfile); $datafile = fopen($tempfile, 'w'); foreach ($page as $line) { echo($line); fwrite($datafile,$line); } fclose($datafile); So I basically just write the fetched HTML line by line to a local file. The weird thing is I'm getting a bunch of ^M characters in the final file after every $line is written to the file.
Fetch A Php File
In my HTML I want to fetch a php file (the php file is a form for a user to fill out) but I want the form pasted on my page but instead it ries to open the form page in a new window of it's own... I tried href but also include.. I don't know that I am using the include properly.. any ideas?
|