1

I am trying to replace parts of the below result set in the $data array, e.g. I want to format price with a thousand separator and to add a variable hyperlink instead of details.

The query itself works and I know how to modify the values as needed but I don't know how to replace the values in the array or better only pass the modifed values to the array.

Note: I need to do this in PHP (not in mySQL directly).

$stmt = $conn->prepare("SELECT itemId, price, details FROM offers WHERE itemId LIKE 'DE%' ORDER BY itemId");
$stmt->execute();
$resultX = $stmt->get_result(); 
$rowCountX = $resultX->num_rows;
$data = array();

while($rows = $resultX->fetch_assoc()) {
    $data[] = $rows; // array that I want to change
}

Update:

Example:
itemId: b500 -> should be b500 // unchanged
Price: 1500 -> should be 1,500
Details: Item 123 -> Item 123

Can someone help me with this ?

Thanks, Tom

2
  • can you add sample input and output. Commented Nov 15, 2020 at 17:56
  • @aviboy2006: Thanks. It's a general question but I provided some examples. Commented Nov 15, 2020 at 18:03

2 Answers 2

2

Not sure if i understand the issue, but if you want to change the values before "display", or return a to view, on your code just do something like this:

$stmt = $conn->prepare("SELECT itemId, price, details FROM offers WHERE itemId LIKE 'DE%' ORDER BY itemId");
$stmt->execute();
$resultX = $stmt->get_result(); 
$rowCountX = $resultX->num_rows;
$data = array();
$rows = $resultX->fetch_all(MYSQLI_ASSOC);
foreach($rows as $row) {
    $row['price'] =  number_format( $row['price'] , 2 , "" , "," );
    $row['details'] = '<a href="#linkhere">'.$row['details'].'</a>';
    $data[] = $row; // array that I want to change
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can do like this : $data = array();

while($rows = $resultX->fetch_assoc()) {
    $input_data = array();
    $input_data['price']= formatIndian($rows['price']);
    $input_data['details']= "<a href='' > ". $row['details']."</a>";
    $data[]= $input_data;
}

Note : formatIndian its just any function which covert number to Indian format

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.