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

  • SEARCH
  • Home
  • 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 6196337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:37:33+00:00 2026-05-24T03:37:33+00:00

I am trying to save an object with a variable amount of cols. The

  • 0

I am trying to save an object with a variable amount of “cols”. The number of cols is equal to the number of headers. This is how the code looked before:

if(isset($_POST['submit'])){
    $sub = new Sub();
    $sub->product_id = $_POST['product_id'];

    $sub->col1 = $_POST['col1'];
    $sub->col2 = $_POST['col2'];
    $sub->col3 = $_POST['col3'];
    $sub->col4 = $_POST['col4'];
    $sub->col5 = $_POST['col5'];
    $sub->col6 = $_POST['col6'];
    $sub->col7 = $_POST['col7'];
    $sub->col8 = $_POST['col8'];
    $sub->col9 = $_POST['col9'];
    $sub->col10 = $_POST['col10'];
    $sub->col11 = $_POST['col11'];
    $sub->col12 = $_POST['col12'];
    $sub->col13 = $_POST['col13'];
    $sub->col14 = $_POST['col14'];
    $sub->col15 = $_POST['col15'];

This is how I want it to look:

if(isset($_POST['submit'])){
    $sub = new Sub();
    $sub->product_id = $_POST['product_id'];
    $i = 0;
    foreach($headers as $header){
        $i++ ;
        $sub->col.$i = $_POST['col'.$i];
    }

How do I pass the variable $i into the object’s attributes? $sub->(col.$i) ? $sub->(col{$i}) ? Please help me figure this out =) Thank you

  • 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-05-24T03:37:33+00:00Added an answer on May 24, 2026 at 3:37 am

    Try this:

    $sub = new Sub();
    $sub->product_id = $_POST['product_id'];
    for($i = 1; $i <= count($headers); ++$i)
       $sub->{'col' . $i} = $_POST['col' . $i];
    

    But, this is really not the way that the columns should be stored in the Sub object, you should use an array:

    $sub->columns = array();
    for($i = 1; $i <= count($headers); ++$i) {
        $sub->columns[] = $_POST['col' . $i];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This was my first time ever trying to save object in a file, so
I'm trying to save some code with the following. I've an object with variables
Does it make sense to create new object before trying to save object that
I am trying to save a custom object as a user setting in a
I am trying to save images using the following code: - (void)writeData{ if(cacheFileName==nil) return;
I'm trying to save an object, I googled the HowTo and got a tutorial
I'm trying to save a simple object in the database, but it's giving me
I'm trying to save new value for an existing core data object: [object setValue:Value
I'm trying to save a new object from a django model using a POST
Im trying to save a bitmap jpg format with a specified encoding quality. However

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.