PhP Associative Array displayed in a Table - php

I am VERY new to PhP. Quick question that I am having trouble finding the answer to online, although I am sure most of you will quickly know the answer. I have the following code creating an associative array and then I am trying to display it in a table. I know there are easier ways to display it in a table like a foreach loop, but I would like to learn this method first :
class CarDealer extends Company {
var $navbar_array = array();
function create_navbar_array ( ) {
$mainurl = $this->company_url; // get the main url address of this web page
$this->navbar_array = array( "Home Page"=>"$mainurl?whichpage=home", "Sales"=>"$mainurl?whichpage=sales",
"Support" => "$mainurl?whichpage=support", "Contacts" => "$mainurl?whichpage=contact" );
}
function getLeftNavBar() {
$data ="<table border='1' style='background-color:yellow; width:35%'>";
$data .="<tr><td>$this->navbar_array['Home Page']</td></tr>";
$data .="<tr><td>$this->navbar_array['Sales']</td></tr>";
$data .="<tr><td>$this->navbar_array['Support']</td></tr>";
$data .="<tr><td>$this->navbar_array['Contacts']</td></tr>";
$data .="</table>";
return $data;
}
}
Later in my code I create an object for my class and then try to print the table. Unfortunately I am just getting an output of things like Array['Home Page'].
$carobject = new CarDealer();
$carobject->create_navbar_array();
print $carobject->getLeftNavBar();

you need to pass array values to the function try
class extends Company {
var $navbar_array = array();
function create_navbar_array ( ) {
$mainurl = $this->company_url; // get the main url address of this web page
$this->navbar_array = array( "Home Page"=>"$mainurl?whichpage=home", "Sales"=>"$mainurl?whichpage=sales",
"Support" => "$mainurl?whichpage=support", "Contacts" => "$mainurl?whichpage=contact" );
return $this->navbar_array;
}
function getLeftNavBar($arr) {
$data ="<table border='1' style='background-color:yellow; width:35%'>";
$data .="<tr><td>".$arr['Home Page']."</td></tr>";
$data .="<tr><td>".$arr['Sales']."</td></tr>";
$data .="<tr><td>".$arr['Support']."</td></tr>";
$data .="<tr><td>".$arr['Contacts']."</td></tr>";
$data .="</table>";
return $data;
}
}
$carobject = new CarDealer();
$arr = $carobject->create_navbar_array();
print $carobject->getLeftNavBar($arr);
or need to make public your array
public $navbar_array = array();

before return $data;, print_r($data); so you can know the associative array's structure. If it didnt print on the page, try looking in view-page-source. You'll get an clear picture. I think $data should be an array, so prob it should be like
$data['someField'] = $someValues;
And, Create or define the array before you use them.

You need to concatenate the array values inside the getLeftNavBar() method so php knows to parse them.
try
$data .="<tr><td>{ $this->navbar_array['Home Page'] }</td></tr>";
or
$data .="<tr><td>".$this->navbar_array['Sales']."</td></tr>";
Why not just call create_navbar_array() inside getLeftNavBar()
function getLeftNavBar() {
$this->create_navbar_array();
.....
You could rewrite it like this
class CarDealer {
private function getUrls() {
return array(
"Home Page" => "$this->company_url?whichpage=home",
"Sales" => "$this->company_url?whichpage=sales",
"Support" => "$this->company_url?whichpage=support",
"Contacts" => "$this->company_url?whichpage=contact"
);
}
public function getLeftNavBar() {
$data ="<table border='1' style='background-color:yellow; width:35%'>";
foreach($this->getUrls() as $url ) {
$data .="<tr><td>".$url."</td></tr>";
}
return $data . "</table>";
}
}
$carobject = new CarDealer();
print $carobject->getLeftNavBar();

Related

Larvel Update Data from Array with foreachloop

Hello There I need Help To Update on Vehicle Wies on Specific Dates.I'm Attaching Form image and Here is my Controller code.
public function add_vehicle_expense(Request $request)
{
$veh = array('veh_reg_num' => $request->veh_reg_num);
foreach ($veh as $data) {
print_r($request->date[$data]);
dd();
$veh = Sale_report::where('date',$request->date[$data])->where('veh_reg_num', $request->veh_reg_num[$data])->update(['diesel_qty'=> $request->qty[$data], 'diesel_rate'=> $request->rate[$data], 'diesel_amount'=> $request->total_amount[$data], 'other_expense'=> $request->other_exp[$data]]);
}
if ($veh) {
return redirect()->back()->with('Data Store Successfully');
}
//$veh = Sale_report::where('date',$request->date)->where('veh_reg_num', $request->veh_reg_num)->update(['diesel_qty'=> $request->qty, 'diesel_rate'=> $request->rate, 'diesel_amount'=> $request->total_amount, 'other_expense'=> $request->other_exp]);
/* foreach ($request->date as $reg_num => $key ) {
$s = Sale_report::where('date',$request->date[$reg_num])->where('veh_reg_num',$request->veh_reg_num[$reg_num])->first();
$s->diesel_qty = $request->qty[$reg_num];
$s->diesel_rate = $request->rate[$reg_num];
$s->diesel_amount = $request->total_amount[$reg_num];
$s->other_expense = $request->other_exp[$reg_num];
$s->update();
} */
}
I have try to Update Data by matching Each Vehicle Number with Date Some times it Show ErrorException Attempt to read property on int,ErrorException Undefined array key "data"
I have Found The Solution of this Problem.
I've use implode(',' , $request->veh_reg_num) Then I use $datas = explode() function then I use foreach() With exploded Vairable as foreach($datas as $data => $value){
match the each row that with $data array then I Update the values to data base }

How to Explode data from database in controller of CodeIgniter?

I have a question, I want to change string data from database using explode, because the data is imploded before when I post it to the database. Here's my controller
public function add_detail()
{
$symptoms_id = implode(',', $this->input->post('symptoms_id'));
$CF_user = implode(',', $this->input->post('CF_user'));
$data = array(
'symptoms_id' => $symptoms_id,
'CF_user' => $CF_user
);
$this->db->insert('consultation_detail', $data);
redirect('User/consultation_result', $data);
}
That function is to implode and it works, but I want to make a function in controller too for explode the data.
I just try like this
public function consultation_result()
{
$data['consultation_detail'] = $this->db->get('consultation_detail')->row();
$CF_user = explode(',', $CF_user);
}
But it said undefined $CF_user , I'm sorry because I'm newbie :(
try this
public function consultation_result($data)
{
return explode(',', $data);
}
and use like this
$data = array(
'symptoms_id' => consultation_result($this->input->post('symptoms_id')),
'CF_user' => consultation_result($this->input->post('CF_user'))
);

How to do an array merge between two functions?

I'm trying to get an array_merge but I can't. Calling the example.php file must also embed the array of the Usage_Module function.
Note: The getFunctionModule function must not be changed.
Surely I'm wrong something in the Usage_Module function, can you help me understand what I'm wrong?
Code example.php
function getFunctionModule() {
require "module.php";
$func = "Usage_Module";
if (!function_exists($func)) {
echo "function not found";
}
return (array) $func();
}
$data = ['status' => 'ok'];
$data = array_merge(getFunctionModule(), $data);
print_r($data);
Code module.php
function Usage_Module()
{
$data = ['licensekey2' => 'ok'];
return (array) $data;
}

Laravel 5 how to manipulate my variable in an for each in my controller and then pass it to my view

I have a database table named 'services'. With the function below I get all the records from that table. This function is located in my ServicesController.php.
public function index() {
$roadmap = Roadmap::all();
return view('services', compact('roadmap'));
}
Now in my view I also do the following:
<?php
foreach($roadmap as $roadmap_item) {
$new = array();
$splitted = explode("|", $roadmap_item->steps);
foreach($splitted as $split) {
$new = explode(":", $split);
}
}
?>
Lets say I get the string 'step1:hello|step2:bye' back from '$roadmap-item->steps'. I split them in substrings with explode etc. This is working by the way.
But is there a way I can manipulate the string in the Controller so my view will be nice and clean without many php code and still remain the variable $roadmap with all the database records.
Kind regards,
Dylan
I think you can do very similar things in your controller. Do foreach in your controller first then make a changes in every item in your collection and pass the new collection.
For example:
$roadmaps = Roadmap::all();
foreach($roadmaps as $roadmap){
$roadmap->something = " the changes you want to do as string " . $roadmap->something;
$roadmap->save();
}
return view('services', compact('roadmaps'));
or
$roadmaps = Roadmap::all();
$new = new Collection();
foreach($roadmaps as $roadmap){
$new[] = array('roadmap' => $roadmap, 'something' => $roadmap->string . " etcetc ")
$new->save();
}
return view('services', compact('new'));

Call a function within a function display both within array

how do I call a function from within a function?
function tt($data,$s,$t){
$data=$data;
echo '[[';
print_r($data);
echo ']]';
if($t==0){
$data[]=$s;
tt($data,'two',1);
}else{
$data[]=$s;
}
return $data;
}
print_r(tt('','one',0));
I want 'two' to be shown within the array like
$o[]='one';
$o[]='two';
print_r($o);
function tt($s, $t, array $data = array()) {
$data[] = $s;
if ($t == 0) {
$data = tt('two', 1, $data);
}
return $data;
}
print_r(tt('one', 0));
This is all that's really needed.
Put the array as the last argument and make it optional, because you don't need it on the initial call.
When calling tt recursively, you need to "catch" its return data, otherwise the recursive call simply does nothing of lasting value.
No need for the else, since you're going to append the entry to the array no matter what and don't need to write that twice.
Try this one (notice the function signature, the array is passed by ref &$data):
function tt(&$data,$s,$t){
echo '[[';
print_r($data);
echo ']]';
if($t==0){
$data[]=$s;
tt($data,'two',1);
}else{
$data[]=$s;
}
return $data;
}
$array = [];
tt($array,'one',0);
print_r($array);
/**
Array
(
[0] => one
[1] => two
)
*/
try this
function tt($data,$s,$t){
global $data;
if($t==0){
$data[]=$s;
tt($data,'two',1);
}else{
$data[]=$s;
}
return $data;
}
print_r(tt('','one',0));
OUTPUT :
Array
(
[0] => one
[1] => two
)
DEMO

Categories