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 7886441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:19:07+00:00 2026-06-03T05:19:07+00:00

I’m new to using PDO and have got a little bit stuck- is there

  • 0

I’m new to using PDO and have got a little bit stuck- is there a way I can add a row to the fetchAll array returned?

I want to use a value in the row and create a new value from it. I hope my example is clear enough-

 class.php
--------------------
class connect
{
//all PDO database connection functions here
}

class Display
{
   function cars($colour)
   {
   $crud = new crud();
   $crud->conn();
   $sql = "SELECT * FROM cars WHERE colour=:colour";
   $stmt = $crud->db->prepare($sql);
   $stmt->bindParam(':colour', $colour);
   $stmt->execute();
   $rows =  $stmt->fetchAll(PDO::FETCH_ASSOC);

 ****WHAT I WANT TO DO (see edit)*****
   foreach ($rows as $row) 
       {
          ADD A ROW CALLED 'newprice' that equals ($row['oldprice'] + 300 / 2)
       }


       $this->results = $rows;
    }
 }


 index.php
--------------------
//other html code...

$D = new Display;
$D->cars('1');
foreach($D->results as $row)
{
?>          

<div class="car">
    <h1>Car Name:</h2>
    <?=($row['model']);?>
    Old Price:
    <?=$row['price'];?>
    New Price:
    <?=($row['newprice']);?>    <- outputting the new row I created
 </div>
}?> 

Can this be done or is there a different more efficient way of doing this?

Sorry, I guess I wasn’t very clear with my example! I want to do a lot more than just mathematically alter the value for the new column- I want to preg_split/run it through another class etc!

Best solution I’ve got so far is to create a new multidimensional array from the PDO provided array and then add/edit that array, like this:

     $new=array();
     $key_loop = 0;
     $row_loop = 0;

      foreach ( $rows as $val )
      {
       $keys = array_keys($val);
       foreach ($keys as $key)
         {                
         $new[$row_loop][$keys[$key_loop]] = $val[$keys[$key_loop]];      
         $new[$row_loop]['new_entry'] = 'this works';
         $key_loop++;
         }       
         $row_loop++; 
      $key_loop = 0;
      }
  • 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-03T05:19:08+00:00Added an answer on June 3, 2026 at 5:19 am

    What I’ve ended up doing is cycling through the rows and sticking them in a new multidimensional array with the extra values I need- might not be the most elegant way but it works for me:

    $new  = array();
    $array_loop = 0;
    
    $sql = "select old_price, item_id from car";
    
    ...
    
    $stmt->execute();
    $rows =  $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    foreach ($rows as $val)
    {
        $new[$array_loop]['item_id']   = $val['item_id'];
        $new[$array_loop]['old_price'] = $val['old_price'];
    
        $newprice = $val['old_price'] + 300 / 2;
        $new[$array_loop]['new_price'] = $new_price;
        $array_loop++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the

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.