Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8829025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:44:24+00:00 2026-06-14T07:44:24+00:00

I found a PHP-based recipe and meal plan script, that I’d like to edit.

  • 0

I found a PHP-based recipe and meal plan script, that I’d like to edit.

There’s a MealPlan Class that allows all users to create their own daily menus/mealplans, by selecting from a list of all recipes found in the database.

I want to switch the table/column names used in the insert statement, based on the level of the user.

The original insert method is:

// Insert Meal Plan
function insert($date, $meal, $recipe, $servings, $owner) {
        global $DB_LINK, $db_table_mealplans, $LangUI;

        $date = $DB_LINK->addq($date, get_magic_quotes_gpc());
        $meal = $DB_LINK->addq($meal, get_magic_quotes_gpc());
        $recipe = $DB_LINK->addq($recipe, get_magic_quotes_gpc());
        $servings = $DB_LINK->addq($servings, get_magic_quotes_gpc());
        $owner = $DB_LINK->addq($owner, get_magic_quotes_gpc());

        $sql = "INSERT INTO $db_table_mealplans (
          mplan_date,
          mplan_meal,
          mplan_recipe,
          mplan_servings,
          mplan_owner)
          VALUES (
          '$date',
          $meal,
          $recipe,
          $servings,
          '$owner')";
        $rc = $DB_LINK->Execute($sql);
        DBUtils::checkResult($rc, NULL, $LangUI->_('Recipe already added on:'.$date), $sql);
}

My idea was to change:

mplan_date,
mplan_meal,
mplan_recipe,
mplan_servings,
mplan_owner

to:

$mplan_date,
$mplan_meal,
$mplan_recipe,
$mplan_servings,
$mplan_owner

and use a switch statement to change the table/column names. But I’ve been told that this sort of thing is frowned upon in OOP. What’s the best way to go about this?

(NOTE: I can post the whole class if need be, but figured this would be enough info)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:44:26+00:00Added an answer on June 14, 2026 at 7:44 am

    I don’t have any code for you, but I’ve done something similar to this recently. Basically I dynamically made the SQL string by concating the string depending on what the user input. Bear with me as I try to type something on the fly.

    $columnsArray = array();
    $sqlCmd = "INSERT INTO";
    
    If (someCondition) {
        array_push($columnsArray, "mplan_date");
    }//Could try turning this into a loop and just push all 
     //applicable columns into the array
    
    $counter = 0;
    
    Foreach ($columnsArray as $columnName) {
        $sqlCmd .= " $columnName";
        $counter++
        If ($counter < count($columnsArray)){
            $sqlCmd .= ",";
        }//This will put a comma after each column with the 
         //exception of the last one.
    }
    

    So I did something like this only with a user defined select statement to retrieve data. Basiclly keep going with the logic until you’ve built a custom string that will be the user defined SQL statement, and then execute it. This includes doing something similar to the above script to concat the values into the string.

    Hope this gives you some ideas.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've implemented a mysql-based session interface in php. I just found out that if
I have written a PHP script based on a piece of code I've found
I wrote a small shell script based on an example I found here: https://bbs.archlinux.org/viewtopic.php?id=36305
I'm using a PHP class based on the one found in this answer to
I once tried to implement Comet in PHP. Soon, I found that PHP is
I found that using Smarty with PHP, sometimes extra time will need to be
I found this random page script which is in PHP file. I'm a bit
I have a php script that returns an echo of 'Hello' when accessed half
I'm writing a PHP-based web application that should work with multiple database systems. The
I'm using a function i found on php.net(i think) to sort an array based

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.