WordPress.org

Ready to get started?Download WordPress

Forums

Need advice to connect 31 custom fields with a date variable (2 posts)

  1. lawrenced
    Member
    Posted 2 years ago #

    Hello, first time I've posted, I usually try to solve problems myself but I have hit a brickwall with my amateur PHP knowledge.

    I hope someone who knows their stuff can advise me on a better way:

    I have developed a website for a freind whos family owns a restaurant http://www.grado.cz.

    Each month there is a list of all the specials for that month (for up to 31 days) which is shown in the specials menu. "http://www.grado.cz/specials_menu/prosinec-2011/". I made this using a custom field panel with 31 custom fields for each day. However things get more complex when she then told me she'd like the special of the day to appear each day on the frontpage. Not being very good at PHP I constructed a simple variable that captures the day of the month then used a SWITCH with 31 CASES to show todays special menu item:

    switch ($gradospecial) {
        case 1:
            $test_special = get_post_meta($post->ID, 'dayone', true);
    echo $test_special;
            break;
        case 2:

    It all works but the problem is that to do this each custom field has to have its own unique ID name. Which means a SWITCH with 31 items!!!

    I'm wondering if anyone can advise me on a better way to do this? As this seems bloated.

    So I guess I'm asking:

    Is there a way to add 31 custom fields all with the same custom field ID to a new ARRAY which would automatically number them 01 > 31 so that the 'Variabe=date' variable could check to see which one it should use and then display the right one depending on the day of the month?

    I've tried to read up on arrays but I think I'm missing some fundamental concept somewhere....any suggestions appreciated...

  2. Mark / t31os
    Moderator
    Posted 2 years ago #

    Just to make sure i follow you correctly, your switch goes something like this..

    switch ($gradospecial) {
        case 1:
            $test_special = get_post_meta($post->ID, 'dayone', true);
    		echo $test_special;
    	break;
        case 2:
            $test_special = get_post_meta($post->ID, 'daytwo', true);
    		echo $test_special;
    	break;
    	...etc..
    }

    And you're looking for a way to trim that down to something more manageable than updating 31 cases inside a switch..

    This is how i'd do it..

    $month_days = array(
    	// Array Key => Meta Key
    	1 => 'dayone',
    	2 => 'daytwo',
    	3 => 'daythree',
    	4 => 'dayfour'
    );

    and so on... for each applicable day

    Then extract from the array to determine which field to pull..

    if( isset( $month_days[$gradospecial] ) ) {
    	$special = get_post_meta( $post->ID, $month_days[$gradospecial], true );
    	if( !empty( $special ) )
    		echo $special;
    }

    Hope that helps.. :)

Topic Closed

This topic has been closed to new replies.

About this Topic