Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
  Advanced Search
  HOME    TRACKER    PHP


SuperbHosting.net have generously sponsored dedicated servers to ensure a reliable and scalable dedicated hosting solution for BigResource.com.





Trying To Return Mysql Array Items From Function


I am writing a calendar script which returns events, holidays etc.. from MySQL for days with events. This was easy BUT then I tried to return multiple events on some days and thats where I am stuck.

I found some examples of returning arrays from functions from functions but they all use some form of array(Ƈ',ƈ',Ɖ') and then use a for loop... I am using mysql_fetch_array so this does not seem to work... First, here is some of the code from the function where I am returning the value: PHP Code:




View Complete Forum Thread with Replies

Related Forum Messages:
Function() Return Array?
If i put print instead of $output[] it works. But if I do count $output I get 0. why does it do that? i cant print $output[0] or [1] PHP Code:

View Replies !
Return Array From A Function..
I have a function which querys MySQL table i have implemented. I used to have it all in the html page however decided to make my code better and continue learning by writing this as a seperate function with parameters passed into it, and then calling the function from the html page.

The issue here however is while the function works, when i want to display parts of the data at a later stage in html page it does not work.

The code that fetches the values for displaying is in the function. While i am trying to display the info in the array inside the html page within tables.

I presume its because my function does query and then does this

$data = mysql_fetch_array( $sql ); //all inside the seperate function

and then i have to return $data as a means of passing it into the HTML page when i call the function. Therefore when i am inside the html page there is actually a $data that exists??

That is what i have deduced as being the problem however i can seem to find out how to return this $data or catch it in the html page!

View Replies !
Return Array From Function
I want a function that is able to return a array. But the php-manual says that you can’t.
Ex. of what I want:

function arrFunc()
{
$returarray = array(“some”,”arrray”,”stuff”);
return($returarray);}

View Replies !
Can I Return Array With A Function ?
i try to build a fetch_col function i give in parameter the query result and the number of the col which i want to return i fill an array with result in the function but i don't know how i can return the array i try : Code:

View Replies !
Return An Array From A Function
how should i return an array from a function?

View Replies !
Is It Possible For A Function To Return An Array?
Is it possible for a function to return an array? I've created a function that does a SELECT from the DB. Then I need to echo the returned values. Trouble is, its just the one variable that is being returned. I need all the variables to be returned. The code below does not work. -- code --

function values($ID)
{
$SqlSelectQuery =
("
SELECT
name,
filename
FROM
table
AND
ID = '$ID'
");

..................

View Replies !
Return An Array From Function
I am building a class to interface with MySQL(mostly for learning purposes). I have a function called getFirstRow(), which, as the name implies, is supposed to return an array of the first row. I am gettin gthis error when using it though. "Parse error: parse error, unexpected T_OBJECT_OPERATOR, expecting ')'" Here is the class PHP Code:

class dbConTran{

    var $con;
    var $numRows;
    var $result;
    var $resultArray;
    var $firstRow = array();
    var $lastRow = array();
     
     
     // Open Database connection ---------
     function dbOpen($name,$host,$user,$password){
        $this->conn = @mysql_connect($host, $user, $password)or die ("Error Connecting to Database!");........

View Replies !
Check If Any Items In An Array (php) Is In A Table (mysql).
I want to check if any of the items in a PHP array exist in a MySQL
table. What is the best way to do this with making repeated calls to the
database for each item?

Actual application is:

I have an array (in PHP) of serial numbers of units about to be
shipped.I want to check the ship record (a table in a MySQL database) to check
if that serial number has been used before.

According to our quality policy we never reuse serial numbers, serial
numbers are unique. So prior to printing the packing slip I want to
warn the shipping guy/gal a serial number may be incorrect.

I could brute force it and increment through the "to be shipped array"
and check each item against the table. But that is, as I said brute
force, and I am having to be concerned with system speed lately.

View Replies !
Attempting To Return Values From Array From W/in A Function
I am attempting to read through a text file & then update an HTML page
table w/ the output from the text file (ie; statusing by table).

What I want the code to do is read through the file, look for a line
that matches a couple of strings, next is loop through the file from
that match to 30 lines searching for a "score" from, then store that
score; and then look for the next algorithm (8 of them) run & score.

What I get is an array w/ only the most recent array entry.

I have tried several things; return(), array_push(), php manual, and a
more than a few days searching online for a similar example.

Code example below;

function fraction_0($file,$log) {
while (! feof($file)) {
$line = fgets($file,512);
if (ereg($_GET[what],$line) && ereg("_A_0",$line) &&
ereg("Request ID",$line)) {
print ("$log".htmlspecialchars($line)."<BR>");
for ($i30 = 1; $i30 < 30; $i30++){
$line = fgets($file,512);
if (!ereg("returned best solution",$line)){
if (ereg("Score from run", $line)){
print ("$log".htmlspecialchars($line)."<BR>");
$alg_score = explode(" ", $line, 512);
if (ereg($alg_score[6], 1)) {
$alg_array = array("alg0_1" =$alg_score[8]);
print "score for alg 1 is $alg_array[alg0_1]<BR>";
}elseif (ereg($alg_score[6], 2)) {
$alg_array = array("alg0_2" =$alg_score[8]);
} elseif (ereg($alg_score[6], 3)) {
$alg_array = array("alg0_3" =$alg_score[8]);
} elseif (ereg($alg_score[6], 4)) {
$alg_array = array("alg0_4" =$alg_score[8]);
} elseif (ereg($alg_score[6], 5)) {
$alg_array = array("alg0_5" =$alg_score[8]);
} elseif (ereg($alg_score[6], 6)) {
$alg_array = array("alg0_6" =$alg_score[8]);
} elseif (ereg($alg_score[6], 7)) {
$alg_array = array("alg0_7" =$alg_score[8]);
} elseif (ereg($alg_score[6], 8)) {
$alg_array = array("alg0_8" =$alg_score[8]);
}
print "<table border="1"><tr><th>Alg 1</th><th>Alg 2</
th><th>Alg 3</th><th>Alg 4</th><th>Alg 5</th><th>Alg 6</th><th>Alg 7</
th><th>Alg 8</th></tr>";
print "<tr><td>$alg_array[alg0_1]</td><td>
$alg_array[alg0_2]</td><td>$alg_array[alg0_3]</td><td>
$alg_array[alg0_4]</td><td>$alg_array[alg0_5]</td><td>
$alg_array[alg0_6]</td><td>$alg_array[alg0_7]</td><td>
$alg_array[alg0_8]</td></tr>";
print "</table>";
}
} else {
print ("$log".htmlspecialchars($line)."<BR>");
$i30=30;
}
}
}
}

View Replies !
Recursive Function Should Return An Array Of Results
im trying to write a recursive function in php where this function should return an array of results..here is my codes:

function comfibonacci2($limit){ //using recursive function
  if ($limit <= 1){
    return 1;
  }
  return (comfibonacci2($limit-1) + comfibonacci2($limit-2));
}

this is actually a fibonacci function. i want it to run same as its iterative function :

function comfibonacci($limit,&$fib){ //using iterative function
  $fib[0]=1;$fib[1]=1;
  for ($i=2; $i<=$limit;$i++){
    $fib[$i]=$fib[$i-1] + $fib[$i-2];
  }
}

can anyone correct my recursive codes?
thanks c",)

View Replies !
Recursive Function - How To Save The Results From All The Loops And Return Them All As An Array.
I have this recursive function that seems to get what I am looking for when I echo from the function but I don't understand how to save the results from all the loops and return them all as an array.

I tried a few ways which I removed from the code to clean it up but none of them even came close to working. Code:

View Replies !
Function Doesn't Return If "load" Is An Array Value
I have created a simple function to load an array with name / value pairs, and return the array. Most of the time it works as expected, but one particular name / value pair causes the function not to return. No errors are reported.

My system details:

OS: Windows XP SP2 ("Windows NT OFFICE 5.1 build 2600" reported by phpinfo)
Server: XAMPP for windows (apache)
PHP Version: PHP Version 5.1.4

The function: Code:

View Replies !
How Many Items Are In An Array?
I dont know how many values there are in my array after it's been split.. is there a command? array_count_values doesnt do what I need sadly.

View Replies !
Getting The Items Into A Session Array
I'm creating a shopping cart and are having trouble getting the items into a session array. Lets say i have a form like: Code:

View Replies !
Display All Array Items
How do I display all the items within an array? Also, how do I delete a particular item from an array?

View Replies !
Compare Items In An Array...
I have been getting on fine with basic coding until I stumbled accross a problem. I cant seem to figure out how to check to see if a value in one array is in another array - and if so just simple report back "true" or "false".

Lets call the two arrays LOCKS and KEYS. Doing a print_r($locks) would give: Code:

View Replies !
How Do I Delete Items From Array?
I can add several items to the basket, but how do I delete one from the basket? I want to delete any of the added items. Here is my code for creating the array and adding items to it.

// creating the array
$item = array();
$item['id'] = "1";
$item['price'] = "100";
$item['quantity'] = "1";

// add item
$_SESSION['basket'][] = $item;

View Replies !
Remove Items From An Array.
I have a form which contains a checkbox for every item in my array; when checked it will list the values. I want to delete all items from the array, which have been checked.

I've used many loops; and var_dump, no sucess I then tried unset(); with loops and again no luck. Its possible. but I just need a hint on how to do it.

View Replies !
Unset Array Items
I want to do this...

unset($dirarray['css'], $dirarray['Templates']);

but it doesn't work, whereas this does

unset($dirarray[0], $dirarray[1]);

Any way I can get the former to work?

View Replies !
Rearranging Array Items
I asked a question on the forum a few days ago about swaping items in an array and the answer was pretty simple... but now my problem is a little bigger. Now I need to actually rearrange the items in an array like this:

array[0] = apples
array[1] = oranges
array[2] = grapes
array[3] = bannana
array[4] = pear
array[5] = grape fruit

^^how can I turn that..... into this:

array[0] = grape fruit
array[1] = apples
array[2] = oranges
array[3] = grapes
array[4] = bannana
array[5] = pear

Basically grape fruit was moved to the top of the array index and everything else was moved down automatically.

View Replies !
Removing Items From An Array
I am using mySQL for my news tables.. some of the headlines field in my table contain the exact same content (but they are from a different source so I need to keep them in the table). Unfortnately, I don't want to display the articles with the same headlines. so how do I remove rows from my query array that have the identical headlines?

$sql = "SELECT id, headline FROM newsTable WHERE headline LIKE '%$userChannel%' OR body LIKE '%$userChannel%' LIMIT 10";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)){

echo '<p><a href="http://www.foo.com/thread.php?id='.$row[id].'">'.$row[headline].'</a></p>'

}

I tried SELECT DISTINCT in mySQL but it doesn't work.. so there must be a PHP solution to compare the headline field for each row and not display like headlines.

View Replies !
Adding Items To An Array
I have a for loop to add items to an array, but when i use a for loop to print these items, iI only get the last item of the array. This is a simplified example of what i do, what is wrong with it:

for ($i=0;$i<10;$i++)
{
$test=array($i=>$i);
}

for ($t=0;$t<10;$t++)
{
echo "array".$t."= ".$test[$t]."<BR>";
}

View Replies !
Adding Only Unique Items To An Array
I've made a small code snippet that adds a new item ($value = $_GET) to an array in a cookie. My main question is in the title of the post, and I have a related one: How do I delete a specific value from the array and shift all the other values down to fill its place? Will this happen on its own? And one more: Using this code I display the array values in descending order;

foreach ($_COOKIE['best'] as $name => $value) {
$best = urlencode($value);
echo //insert code here
}
}

Is there any way to display this in ascending order?

View Replies !
Counting Items In An Array Having X Attribute
I want to randomly select 5 cards from a standard playing card deck and count how many of each suit are returned:

[connect]
$sql = "SELECT * FROM cards ORDER BY RAND()LIMIT 5";
$result = mysql_query($sql);
while($cards = mysql_fetch_array($result)) {

$value = $cards["card_value"];
$suit = $cards["card_suit"];
$image = $cards["card_image"];
--------

Using count() within the loop returns a count of 1 since each card can only be of one suit. I tried using something like this:

$spades = 0;
if($suit='spades') {
$spades = $spades+1;
}

and I tried a foreach statement to count within the loop which always returns a count of 1.

Would someone be so kind as to point me in the right direction?

View Replies !
Problem Adding Items To An Array
I have been trying to learn php for the last 3 weeks or so and I have a problem I cannot work out. I am trying to create a list (shopping cart of items) which are contained within an array and which can have new items added via a form. I then just want the contents of the array to be displayed in a simple list.

I can get one item to be added, but when I try to add another item, the new item just replaces the old one. Code:

View Replies !
Function/Global Var To Return Name Of Calling Function?
I'm sure I saw this somewhere but can't remember where and can't find it now...

Is there a PHP function or global variable that will return name of the calling function? I want to do this for error reporting purposes without
having to hardcode the function name into my scripts.

Say the function is named getFunctionName(). I want to do something like...

function foo() {

View Replies !
Remove Items From Array, Shift Positions
Hey, lets say I have:

x = array("red", "blue", "green", "yellow", "orange", "grey");

Now lets say I have a loop that prints out what is in the array. I want to be able to remove blue green and yellow, but effectively shift down everything else in the array (in this case orange and grey) so that orange is now positioned relative to where blue was and grey is where green was. By this I mean x[1] = "blue" and x[2] now quals "grey".

How can I do this?

View Replies !
Loop, Post Then Show Array Items
i have a page that returns a bunch of rows with some data from a query.

i've added an input box to the row along with a counter (input type="text" name="text<?=$counter; ?>") so that when the page loads, all the textboxes that appear become part of an array (text1, text2 and so on)

when the page loads, the rows are populated with data, except for these input boxes which are empty.

the user then selectes whatever rows are required (lets say 1,3 and 5) and enters data in these empty inputboxes, then submits the page Code:

View Replies !
MySQL Result To Real Array In Function
I'd like to create a function which input is the result of a mySQL
query.
The output should be exactly the same, only not a mySQL result array,
but a 'real' array.
So it should also get the fieldnames returned by mySQL and use those as
keys.

I can't get things to work properly: it should return a
multidimensional array,
like

$result_array[1] = array(
[field1] => field1 value,
[field2] => field2 value,
etc.
)

somehow my result is (with code below)

$result_array[1] = array(
[0] => field1 value,
[field1] => field1 value,
[1] => field2 value,
[field2] => field2 value,
etc.
)

+++++ code ++++++

$get_res= mysql_query(QUERY);

if( $res = mysql_fetch_array( $get_res ) )
{

do{

$result[] = $res;

}while( $res = mysql_fetch_array( $get_res ) );

};

foreach( $result as $key => $value ){

print_r($value);

};

View Replies !
Top Items In Mysql
I have a database I store stats in for views.. but I am not sure how to find the top 5 or so viewed items. Here is the table structure:

CREATE TABLE `item_views` (
`id` int(8) NOT NULL auto_increment,
`myid` int(8) NOT NULL,
`ip` varchar(14) NOT NULL,
`date` date NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `item_id` (`myid`,`ip`)
)

It has quite a few rows for each `myid`. Did I set this up wrong to ge the top items? I'm guessing I need to group the `myid`'s but I would like to know the proper or most efficient way to store stats, since i need the top items frequently.

View Replies !
Displaying Items From MySQL
I just noticed that there is a problem with it. When I search for 'wheels', there are 200 results. When I search for 'wheel' there are 400 results. I have both words, wheel and wheels, in my keywords table.

Is there something I can do in my query SELECT statement or my while loop to eliminate duplicates? Code:

View Replies !
Return $$ From Function
How can I use the variables generated by the following function in my code? They're just not being passed to the main script. I've tried global $$k; bu that did nothing PHP Code:

View Replies !
To Return A Function?
If you guys have a function that doesnt return anything, do you still include a 'return;' statement at the end? Is it good practice todo so or pointless?

View Replies !
Function Return
I have created an image resize code into a function which saves me outputing the code four times as my form has four image inputs.

Anyway at then end of this function I have an echo section that displays the image resized ( 4 in all images as function is called 4 times).

Anyway I would like to return the image name after the function is run, but not sure how to get this info. Code:

View Replies !
What Is A Return In Function ?
I know this is a wage question, but since iam new to the programming world jsut wanted to clarrify this silly doubts. Why do we need return in the function when we can use echo and get the output? Can someone explain me more abt funtions ?

View Replies !
Cannot Use Function Return Value
if(empty(trim($_POST["act_title"]))){
$error .= "<p>the title should not be empty!</p>";
}

is there anything wrong with the Sentence

if I remove the trim,it works ok.

View Replies !
Function Return()
if you have return $varname; in a function call, how do you then use that variable?

ex:

function test() {

$sql_query = 'select * from table'

return $sql_query;

}

is it possible to return a query like this? basically i want the function to simply query the database based on passed info, and have the parsing be handled by the calling page.

View Replies !
Return Value From Function
i've got a function which insert data into database. so if the transaction is successful, return a value. after that redirect user to another page. here is code:

View Replies !
Is Return In Function A Must
I have something like this

<?

$array1 = array("dir1", "dir2", "dir3");

$array2 = array("file1", "file2", "file3");

//function to do the same
function menu($haystack){
foreach ($haystack as $needle){
echo $var;
}
?>

I'm including that in my index and just echoing (echo menu($array1))

Now that function doesn't have return $var, and that worries me. Do functions always need to return something, or?

Is there maybe a better way to do this, so that i output it with simple echo, but again keep function propertie?

View Replies !
Can't Use Function Return Value
getting this error: Fatal error: Can't use function return value in write context in C:Program Filesxampphtdocsentries.php on line 96

this is the code, starting at line 95:
$tsid_rsDetails_total = "-1";
if (isset($_COOKIE('timesheetid'))) {
  $tsid_rsDetails_total = (get_magic_quotes_gpc()) ? $_COOKIE('timesheetid') : addslashes($_COOKIE('timesheetid'));
}
mysql_select_db($database_conn_org, $conn_org);
$query_rsDetails_total = sprintf("SELECT timesheet_entries.id_timesheets_main, (Sum(timesheet_entries.hours) + Sum((timesheet_entries.minutes)/60)) AS "rTotal" FROM timesheet_entries WHERE id_timesheets_main = '%s' GROUP BY timesheet_entries.id_timesheets_main ", $tsid_rsDetails_total);
$rsDetails_total = mysql_query($query_rsDetails_total, $conn_org) or die(mysql_error());
$row_rsDetails_total = mysql_fetch_assoc($rsDetails_total);
$totalRows_rsDetails_total = mysql_num_rows($rsDetails_total);

?>
any ideas?

View Replies !
Return In Function
function Adding($number) {
$number = $number+5;
return $number;
}
<br>
$number = 5;
$newnumber = Adding($number);
<br>
echo "$newnumber";

I was reading this tutorial but I never understood why and what return does.

View Replies !
Function Return Value
PHP Code:

if(strlen($_POST["room"]) > 1 && strlen($_POST["month"]) >= 1 && strlen($_POST["year"]) = 4){
include_once("calendar.inc.php");
calendar($_POST["room"], $_POST["month"], $_POST["year"]);
}

i'm getting an error: Fatal error: Can't use function return value in write context in.

View Replies !
Function To Return More Then One Value
is there anyway a function could return multiple values (like the function looks up in a recordset and returns the recordset).

View Replies !
Show Two Items In A Row When Items Are Picked Randomly?
I've looked at this inside and out to figure out how do a random twice so that it has a much less likely chance to show the same item twice in a row. But rand(1,3) doesn't seems to let you put variables in them $, only numbers.

how to make this a lot less likely to show two items in a row when items are picked randomly? I even tried this below, and if two numbers are the same then it shows the higher numbers, but if they are not the same then they show one of the first calls for the random items.

Thing is, if they are not the same and it shows the first calls for a random number, its back to the same problem again of showing too many same items again. Anyone know of some easier and more effective ways of doing this? Code:

View Replies !
MySQL Sytax For Selecting Items
how to use an 'OR' statement with MySQL to select a range of values:

SELECT * FROM table WHERE staffid >=26 OR <=65

This doesn't work but I thought it might. I want to select those in the table with staff if 26 to 65.

View Replies !
How To Return An Image From A Function?
Currently i have a script that creates an array of data points. This array is then sent to a function to create a graph of the points in the array. My problem is this:

If i use the script as a php script on its own it will create the graph and i can display it using <img src= blah.php>.
However, i changed this so the graph script was a function but this outputs the jpeg code for the graph rather than the pic of the graph. i am putting this down to the fact that php cannot use embedded graphics (hence the reason i used <img src>).

So, i know the code actually creates the graph but i cannot figure out how to return the image to the main script to display the graph.

$pic = imagejpeg($image);
return($pic);

ie, create the pic and save it as a variable to be returned and then displayed...doesnt work!

i have also tried returning $image and then using imagejpeg($image) in the main script...which obviously doesnt work either. I have tried a couple of other things as well but they were equally unsuccesful. I reckon all this is probably as clear as mud so if any clarification is needed please ask!!

View Replies !
Function Return Values
If I assign a variable to a function, say,

$x = f($y);

and then f happens not to return anything, does $x just remain unset? Or is
this illegal?

View Replies !
Problems With Function Return Value
[color=blue]
> Help, I'm stuck! I've written the below function as part of my form data
> filtering steps and for some reason It won't return the right value![/color]

All of these lines:

$filter[] = strpos('to:', $post);

will add another array entry even if the function returns nothing, so
your end test is always true. Recode.

View Replies !
Using A Function To Return A Boolean
I am using a function in which I want to return a boolean and I want a variable set that I can use in the rest of the program.

Returning a boolean isn't much of a problem, but how can I set a variable called "$error" that can be used in the whole program? Now I only can use the variable within the function, how can I solve this?

View Replies !
Return Or Echo In Function
i have a function, but what I want to know is when should i use return? and when should i use echo?

View Replies !
SNMP Function Return
snmpget() - if no connection can be made to host a fatal error is encountered instead of returning false. How can I avoid this problem? i tried:

snmpget(...) or die (message); // still fatal error instead of message output

View Replies !
Return Function Loop
I have a function that returns all the products with the same ItemId. So I will usually have more than 1 row. The problem is I want to display the info from row one different than the rest. basically after I display the first row of product info - I just want a list of the other product names below: like:

  <td colspan="2"><?php 
for($i=0; $i<$numPd;$i++)
{
extract($product[$i]);
 echo $Name ."<br />"; 
}?></td>

but it complains that my array is now empty?? maybe because I already extracted it for the first product? Code:

View Replies !

Copyright © 2005-08 www.BigResource.com, All rights reserved