blue_seraph Posted March 30, 2006 Share Posted March 30, 2006 I need to get simular data from a data base on many pages on my siteI descided to make a function so I wont need to right the same code over and overI know the function works i just dont know how to get the data outfunction getAllItems($what,$where,$ordered){ $query = "SELECT * FROM `items` where `$what`= '$where' ORDER BY '$ordered'"; $result = mysql_query($query); $i=0; while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ $id[$i]=$row[id]; $cat[$i]=$row[category]; $pName[$i]=$row[prodname]; $smallD[$i]=$row[smalldescript]; $longD[$i]=$row[longdescript]; $pImage[$i]=$row[image]; $price[$i]=$row[price]; $i++; } $gAI=$i;}--------------------------------------------------This is how i am retreiving the data--------------------------------------------------getAllItems("category","Hookah Flavors","prodname");<?for ($i=0;$i<=$gAI;$i++){?><li><a href="flavors.php?id=<?=$id[$i]?>" target="hookahflavors"><li><?=$pName[$i]?></a></li><?}?>--------------------------------------------------The data being retrieved is product infoHow do i save the arrays from the function so they can be used afterwordsI want to use this funtion to create dynamic index pages for all the categories-------------------------------------------------- Link to comment https://forums.phpfreaks.com/topic/6144-creating-a-function/ Share on other sites More sharing options...
akitchin Posted March 30, 2006 Share Posted March 30, 2006 this is a fairly basic part of functions - i would suggest reading a little more about custom functions.what you're after is the return statement. when you return something, it is sent back as info to the scope that called it. for example:[code]function two_plus_two(){ $value = 2 + 2; return $value;}$four = two_plus_two();[/code]by returning $value in the function, calling two_plus_two() gives $value to whatever it is assigned. it is also possible to use it directly, rather than assigning it to a variable:[code]$eight = 2 * two_plus_two();[/code] Link to comment https://forums.phpfreaks.com/topic/6144-creating-a-function/#findComment-22163 Share on other sites More sharing options...
blue_seraph Posted March 30, 2006 Author Share Posted March 30, 2006 i know i can do $four = two_plus_two();but i have 7 array that the funtion creates that i need to be able to use after the funtion$id[] and $pname[] in this case<?for ($i=0;$i<=$gAI;$i++){?><li><a href="flavors.php?id=<?=$id[$i]?>" target="hookahflavors"><li><?=$pName[$i]?></a></li><?}?>i'll have to use the rest of the arrays latter Link to comment https://forums.phpfreaks.com/topic/6144-creating-a-function/#findComment-22182 Share on other sites More sharing options...
Guest footballkid4 Posted March 30, 2006 Share Posted March 30, 2006 Return all of the arrays in another array:[code]<?phpfunction your_function(){ $id = array(); $pname = array(); //other variables return array( $id , $pname );}?>[/code] Link to comment https://forums.phpfreaks.com/topic/6144-creating-a-function/#findComment-22206 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.