Hi there

How can I loop thru some Database values to recreate following:

$days = array(
				"$dayone"=>array($link,'class',$title),
				"daytwo"=>array($link,'class',$title),
				"daythree"=>array($link,'class',$title)
		    );

Somehow i can't get the foreach function to recreate the three arrays dynamically.

Btw, the whole expression sits within a Loop so, the solution:

$days = array( 
		$dayvalue => array($link,'class', $title),
		);

Didn't work as I end up having 3 Arrays instead of one. I've even tried to only loop the inner array part, but it won't work

Hope this is somewhat understandable..

Thanks and regards for any help...

    $days = array();
    foreach() {
    	$days[] = array($link, ...);
    }
    

      Hi johanafm

      I've tried it with your suggestion, but it doesn't work. It doesn't render anything?
      Do you think it's got to do with the loop?

        It's hard for us to tell where the problem is with so little code to go on. Here's a basic example of how to build a multi-dimensional array while looping through a MySQL DB result set:

        $days = array();
        
        while($result = mysql_fetch_assoc($query))
            $days[] = array($result['link'], 'class', $result['title']);

        Compare that to what you're doing and see if you can spot the problem. If not, we'll need to see more code (e.g. where you're looping through the DB result set and possibly how you're trying to use the array once you've created it).

          Hi bradgrafelman

          Yeah sorry for the sparse info, but I did't want to complicate things up. However I'm using Wordpress as CMS but have created a custom calendar to meet my needs (display dates from Custom Field Values).

          Hoping it might help ill walk you thru my thoughts and decisions.

          The custom PHP calendar needs to be fead with following array which I am trying to reproduce. It's meant to put in manual, and I would like to create it dynamically.

          $days = array(
          					"2"=>array($link,'class',$title),
          					"3"=>array($link,'class',$title),
          					"5"=>array($link,'class',$title)
          		    );
          
          	$time = time();
          	$oldlocale = setlocale(LC_TIME, NULL); #save current locale 
          	setlocale(LC_TIME, 'ch_DE'); #swiss
          
          	echo generate_calendar(date('Y', $time), $month, $days, 2, 'http://somelink', 1);
          	setlocale(LC_TIME, $oldlocale);

          Basically this will call the generate_calendar function with the options set as above, whereas the the top arrays in the array $days will be highlighted in the calendar. This so far works.

          Now I've set some custom fields in Wordpress which I have already called some other place and should work to with following code:

          $calEvents = new WP_Query();
          		$calEvents->query('cat=4'); // wordpress query
          		if ($calEvents->have_posts()): while ($calEvents->have_posts()) : $calEvents->the_post(); // wordpress loop
          		global $more, $post;
          		$customdate = get_post_meta($post->ID, "date_value", true); // calling custom fields
          		list($jahr, $monat, $tag) = explode("-", $customdate); // extract only the day value
          
          	$singletag = intval($tag); // reformat from 02 to 2
          	$link = get_permalink();
          	$title = get_the_title();
          
          	$days = array(  // this returns an array for each value instead one array with nested data as required
          	        $singletag => array($link,'class', $title) 
          	        );
          
                      endwhile; else: endif; // end loop
          

          As far as I'm concerned, the pulling in of the custom values is not the problem, rather the arranging of the data, or the loop.

          Btw. I am aware that this is not a Wordpress forum, but I couldn't been been helped there, unfortunately.
          I hope this gives more information on what I'm trying to achieve!

          Thanks marcel

            Write a Reply...