Jump to content


function question: returning values from dynamically generated/assigned variable

  • Please log in to reply
2 replies to this topic

#1 animus

  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 13 July 2006 - 03:30 PM


I am having trouble returning values from a function that dynamically creates/assigns variables based on database entries. The idea is to have just a page call to the database and have php make my page. However, each page will have a ton of links to other products and parts of the site. Therefore, I just want to have each page’s links/images/resources handled in the following way:

<a title="<? echo $image_description_151; ?>" href="<? echo $image_url_151; ?>"><? echo $image_title_151; ?></a>

I have got this to work when it is not encapsulated within a function.

Any thoughts?


DB: Pages Table
      Links Table
      Images Table
      Resources Table
Pages table houses 1 Primary Key page_id and the foreign keys for the other tables
Links table houses link_title, link_description, link_url, active & page_id (foreign key)
Images and resources tables are exact same structure as links (i.e. resoure_title, images_title)

PHP:1 php file 'dbhook.php' (login information for DB)
        1 php to return values from DB


$myPageData = array("link","image","resource");
$myPageData_length = count($myPageData);

function getData($page,$searchOn){

### Function to retrive all page data from database ####

Date: 07.13.06    
Varible structure:
where NUMBER is the link number on the page & VARIBLE is what section of database to look under, all three should be used on each link line
example: <a title="<? echo $VARIABLE_description_NUMBER; ?>" href="<? echo $VARIABLE_url_NUMBER; ?>"><? echo $VARIBLE_title_NUMBER; ?></a>

//create a plural variable for the DB query
$plural = $searchOn."s";

//include my hook into the database

//connect to mySQL

//select the correct database
@mysql_select_db($database) or die("I am sorry but I do not see the database you requested");

//assign query variables
$query="SELECT aagtest.$plural.".$searchOn."_title, aagtest.$plural.".$searchOn."_description, aagtest.$plural.".$searchOn."_url, aagtest.$plural.active FROM aagtest.pages LEFT JOIN aagtest.$plural ON aagtest.pages.page_id = aagtest.$plural.page_id WHERE aagtest.$plural.page_id =$page";

//perform querys

//set arrays for processing returned results

//determine row amounts

#loop through returned results and dynamically create varibles
#assign these varible names to dynamically created array values


#2 akitchin

  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 13 July 2006 - 05:39 PM

i didn't slog through all your code, but it sounds to me like you'd be better off using an array rather than ending your variables in _number.  perhaps restructure your data as:

$image[NUMBER_here] = array
  'title' => "Image NUMBER's title",
  'description' => "Image NUMBER's description",
  'url' => "Image NUMBER's url"

then you can simply pass through all of the $image array with a foreach and you'll have all your images displayed:

foreach ($image AS $sub_array)
  echo '<a title="'.$sub_array['description'].'" href="'.$sub_array['url'].'">'.$sub_array['title'].'</a>';

simply build the array of info as such in each of your query's rows, assign it to a temporary array value, and make the array of image info arrays as the function's return value.

#3 animus

  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 13 July 2006 - 07:03 PM

Thanks akitchin!

I'm pretty new to scripting so I'm still getting my feet, your example certainly helps me out. I hadn't thought of setting up the array like that. Thanks so much for the help!


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users