Error: 1Helper could not be found. - cakephp - php

I am trying to create a index method for the content model controller and I am getting the following error, I searched here but did not find the relevant answer. Please take a look at the following code and screenshot:
Content.php:
class Content extends AppModel{
public $name = 'Content';
}
ContentsController.php:
class ContentsController extends AppController {
public $name = 'Contents';
public function index() {
$this->set('Contents', $this->Content->find('all'));
}
}
index.ctp:
<h1>View All Content</h1>
<table>
<tr>
<th>ID</th>
<th>Title</th>
<th>Content</th>
</tr>
<?php foreach ($Contents as $content) : ?>
<tr>
<td><?php echo $this->$content['Content']['id']; ?></td>
<td><?php echo $this->$content['Content']['title']; ?></td>
<td><?php echo $this->$content['Content']['content']; ?></td>
</tr>
<?php endforeach; ?>
</table>
Screenshot:
http://postimg.org/image/sslcgoutx/
Please help as I am new to cakephp and learning it.

Try this in index.ctp
<h1>View All Content</h1> <table>
<tr>
<th>ID</th>
<th>Title</th>
<th>Content</th>
</tr>
<?php foreach ($Contents as $content) : ?>
<tr>
<td><?php echo $content['Content']['id']; ?></td>
<td><?php echo $content['Content']['title']; ?></td>
<td><?php echo $content['Content']['content']; ?></td>
</tr>
<?php endforeach; ?>
</table>

Related

Display JSON info in a PHP table

I am trying to display JSON content in a PHP table but its not working. I can't figure out what I should change.
Here is my code:
<html>
<head>
<title>Download</title>
</head>
<body>
<?php
$myData = file_get_contents("https://youtubetoany.com/#api/json/videostreams/VEou0QBeHlk");
$myObject = json_decode($myData);
$myObjectMap = $myObject->vidInfo;
?>
<table>
<thead>
<tr>
<td>Url</td>
<td>Size</td>
<td>Quality</td>
<td>Type</td>
</tr>
</thead>
<tbody>
<?php foreach($myObjectMap as $key => $item): ?>
<tr>
<td><?PHP echo $item->dloadUrl; ?></td>
<td><?PHP echo $item->rSize; ?></td>
<td><?PHP echo $item->round; ?></td>
<td><?PHP echo $item->quality; ?></td>
<td><?PHP echo $item->ftype; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>
This is what I get in my browser:
Url Size Quality Type
Your source has some javascript attached. You need to get rid of that:
$myData = file_get_contents("https://youtubetoany.com/#api/json/videostreams/VEou0QBeHlk");
// get the substring from start til the first occurence of "<script"
$myRealData = substr($myData,0,strpos($myData,"<script"));
$myObject = json_decode($myRealData);
BUT this source doesn't seem to be made to be grabbed. So I won't rely on that source or that it'll stay the way you find it now.
I just found probably why its not working. The link returns a JavaScript attached to the bottom of the JSON. So here is my solution.
<html>
<head>
<title>Download</title>
</head>
<body>
<?php
$myData = file_get_contents("https://youtubetoany.com/#api/json/videostreams/VEou0QBeHlk");
// This up to the last occurrence of the "}"
$json_block = substr($myData, 0, strripos($myData, "}"));
$myObject = json_decode($json_block);
$myObjectMap = $myObject->vidInfo;
?>
<table>
<thead>
<tr>
<td>Url</td>
<td>Size</td>
<td>Quality</td>
<td>Type</td>
</tr>
</thead>
<tbody>
<?php foreach($myObjectMap as $key => $item): ?>
<tr>
<td><?PHP echo $item->dloadUrl; ?></td>
<td><?PHP echo $item->rSize; ?></td>
<td><?PHP echo $item->round; ?></td>
<td><?PHP echo $item->quality; ?></td>
<td><?PHP echo $item->ftype; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>

using variable from array outside of foreach loop

Im trying to display variables from an array. I am using a foreach loop, however I need to display $order['campaign_name'] before the loop so that it only shows up once. How can I do this? If I change it to $orders['campaign_name'] I get an undefined index error.
<div class="table-responsive">
<table class="table" id="component-table">
<?php if ($orders) { ?>
<?php foreach ($orders as $order) { ?>
<thead>
<tr>
<td colspan=100%><h3><?php echo $order['campaign_name']; ?></h3></td>
</tr>
</thead>
<tbody>
<tr class="campaign-list" id="campaign-list">
<td><?php echo $order['component_name']; ?></td>
<td><?php echo $order['component_owner']; ?></td>
<td><?php echo $order['component_date']; ?></td>
<td><?php echo $order['campaign_code']; ?></td>
</tr>
<?php } ?>
<?php } else { ?>
<tr>
<td class="text-center" colspan="8"><?php echo $text_no_results; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
You are trying to get those value which is not exist before loop. You are directly calling VALUE.
Easily just put index value behind tha array
echo $orders[0]['campaign_name']
It will print your value.
You would need need to know which index of the array you want to show, but if you want to show the first index of the array you can use $orders[0]['component_name'].
<div class="table-responsive">
<table class="table" id="component-table">
<?php if ($orders) { ?>
<thead>
<tr>
<td colspan=100%><h3><?php echo $orders[0]['campaign_name']; ?></h3></td>
</tr>
</thead>
<tbody>
<?php foreach ($orders as $order) { ?>
<tr class="campaign-list" id="campaign-list">
<td><?php echo $order['component_name']; ?></td>
<td><?php echo $order['component_owner']; ?></td>
<td><?php echo $order['component_date']; ?></td>
<td><?php echo $order['campaign_code']; ?></td>
</tr>
<?php } ?>
</tbody>
<?php } else { ?>
<tbody>
<tr>
<td class="text-center" colspan="8"><?php echo $text_no_results; ?></td>
</tr>
</tbody>
<?php } ?>
</table>
</div>
Your campaign name continues to echo out because you have it inside of your foreach loop; if you want it to display only once, make sure to place it above your foreach loop.
Here's a less cluttered example to learn from:
<table>
...
<?php
if ($orders):
echo $order['campaign_name'];
foreach($orders as $order):
echo '...'; // other components
endforeach;
else:
echo $text_no_results;
endif;
?>
...
</table>

Diffrent query in one function codeigniter

I use Codeigneter and I make a search function and in my function I want run two diffrent query and I want show them in diffrent table, How I can do that Sorry I can't show the data
This is My code:
My controller
function showDetail(){
// Retrieve the posted search term.
$detailTiket = $this->input->get('ticket_id');
// Use a model to retrieve the results.
$data["result"]= $this->tracking_model->showDetail($detailTiket);
$data1["result"]= $this->tracking_model->showDetail2($detailTiket);
// Pass the results to the view.
$this->load->view('tracking/tiket_detail',$data,$data1);
}
My Model
function showDetail($detailTiket)
{
if($detailTiket==""){
$detailTiket = "";
}
$showDetailTiket=$this->db->query("My Query1");
return $detailTiketDown->result();
}
function showDetail2($detailTiket)
{
if($detailTiket==""){
$detailTiket = "";
}
$detailTiketDown=$this->db->query("My query2");
return $detailTiketDown->result();
}
My view
<table Width='800'>
<?php
foreach($data as $row){?>
<tbody>
<tr>
<td><?php echo $row->ticket_id; ?></td>
</tr>
<tr>
<td><?php echo $row->created_time; ?></td>
</tr>
<tr>
<td><?php echo $row->start_IT; ?></td>
</tr>
<tr>
<td><?php echo $row->estimasi_selesai; ?></td>
</tr>
<tr>
<td><?php echo $row->name; ?></td>
</tr>
<tr>
<td><?php echo $row->description; ?></td>
</tr>
<tr style="background-color: cyan">
<td><b><?php echo $row->Status; ?></b></td>
</tr>
</tbody>
<?php } ?>
</table>
</center>
</div>
<div>
<table Width='1000'>
<?php foreach($data1 as $rows){ ?>
<tbody>
<tr>
<td><?php echo $rows->Tgl_Waktu; ?></td>
<td><?php echo $rows->PIC; ?></td>
<td><?php echo $rows->Tracking_Ticket; ?></td>
<td><?php echo $rows->Keterangan_Ticket; ?></td>
<td><?php echo $rows->File_Pendukung; ?></td>
</tr>
</tbody>
<?php }?>
</table>
You can pass data like Associative array to view
change your controller like this
Controller:
$data["result"]= $this->tracking_model->showDetail($detailTiket);
$data["result1"]= $this->tracking_model->showDetail2($detailTiket);
// Pass the results to the view.
$this->load->view('tracking/tiket_detail',$data);
view:
Table1 :
foreach($result as $row)
{
//for first result
}
Table2:
foreach($result1 as $row1)
{
//for second result
}

Parse error: syntax error, unexpected end of file in [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I am getting the "Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\football\index.php on line 123" error on my webserver (using XAMPP). My code is the following:
<?php
include 'FootballData.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>phplib football-data.org</title>
<link href="./css/bootstrap.min.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div class="container">
<div class="page-header">
<h1>Showcasing some library functions...</h1>
</div>
<?php
// Create instance of API class
$api = new FootballData();
// fetch and dump summary data for premier league' season 2015/16
$soccerseason = $api->getSoccerseasonById(398);
echo "<p><hr><p>"; ?>
<h3>Fixtures for the 1st matchday of <? echo $soccerseason->payload->caption; ?></h3>
<table class="table table-striped">
<tr>
<th>HomeTeam</th>
<th></th>
<th>AwayTeam</th>
<th colspan="3">Result</th>
</tr>
<?php foreach ($soccerseason->getFixturesByMatchday(1) as $fixture) { ?>
<tr>
<td><? echo $fixture->homeTeamName; ?></td>
<td>-</td>
<td><? echo $fixture->awayTeamName; ?></td>
<td><? echo $fixture->result->goalsHomeTeam; ?></td>
<td>:</td>
<td><? echo $fixture->result->goalsAwayTeam; ?></td>
</tr>
<? } ?>
</table>
<?
echo "<p><hr><p>";
// fetch all available upcoming fixtures for the next week and display
$now = new DateTime();
$end = new DateTime(); $end->add(new DateInterval('P7D'));
$response = $api->getFixturesForDateRange($now->format('Y-m-d'), $end->format('Y-m-d'));
?>
<h3>Upcoming fixtures next 7 days</h3>
<table class="table table-striped">
<tr>
<th>HomeTeam</th>
<th></th>
<th>AwayTeam</th>
<th colspan="3">Result</th>
</tr>
<?php foreach ($response->fixtures as $fixture) { ?>
<tr>
<td><? echo $fixture->homeTeamName; ?></td>
<td>-</td>
<td><? echo $fixture->awayTeamName; ?></td>
<td><? echo $fixture->result->goalsHomeTeam; ?></td>
<td>:</td>
<td><? echo $fixture->result->goalsAwayTeam; ?></td>
</tr>
<? } ?>
</table>
<?
echo "<p><hr><p>";
// search for desired team
$searchQuery = $api->searchTeam(urlencode("Real Madrid"));
// var_dump searchQuery and inspect for results
$response = $api->getTeamById($searchQuery->teams[0]->id);
$fixtures = $response->getFixtures('home')->fixtures;
?>
<h3>All home matches of Real Madrid:</h3>
<table class="table table-striped">
<tr>
<th>HomeTeam</th>
<th></th>
<th>AwayTeam</th>
<th colspan="3">Result</th>
</tr>
<?php foreach ($fixtures as $fixture) { ?>
<tr>
<td><? echo $fixture->homeTeamName; ?></td>
<td>-</td>
<td><? echo $fixture->awayTeamName; ?></td>
<td><? echo $fixture->result->goalsHomeTeam; ?></td>
<td>:</td>
<td><? echo $fixture->result->goalsAwayTeam; ?></td>
</tr>
<? } ?>
</table>
<?
echo "<p><hr><p>";
// fetch players for a specific team
$team = $api->getTeamById($searchQuery->teams[0]->id);
?>
<h3>Players of <? echo $team->_payload->name; ?></h3>
<table class="table table-striped">
<tr>
<th>Name</th>
<th>Position</th>
<th>Jersey Number</th>
<th>Date of birth</th>
</tr>
<? foreach ($team->getPlayers() as $player) { ?>
<tr>
<td><? echo $player->name; ?></td>
<td><? echo $player->position; ?></td>
<td><? echo $player->jerseyNumber; ?></td>
<td><? echo $player->dateOfBirth; ?></td>
</tr>
<? } ?>
</table>
</div>
</body>
</html>
How can I solve this?
See alternative PHP syntax. You cannot use it like this <?php foreach ($fixtures as $fixture) { ?> <?php } ?> rather <?php foreach ($fixtures as $fixture): ?> <?php endforeach; ?>
You should avoid this {?> and this: <?php}
You shouldn't put brackets directly close to the open/close php tag, but it separate with a space: { ?> <?php { also avoid <? and use <?php

How to Flight_Name instead of Flight_ID?

Sir I have two tables.. (i-e)
(1) fare(Fare_ID, Flight_ID, Departure_Arrival_Station, Fare
(2) flights(Flight_ID, Flight_Name, No_Of_Seats.
I have foreach all data from *fare table and it gives me Flight_ID, which is correct. Now how should i will display the flight_Name instead of Flight_ID on the screen.. I try on it, this is my code. Thank you..
My Model
function getFlightName()
{
$this->db->select("flights.Flight_ID, flights.Flight_Name");
$this->db->from('flights');
$this->db->join('fare', 'fare.Flight_ID = flights.Flight_ID');
$query = $this->db->get();
return $query->result();
}
My Controller
public function fare()
{
$data['tt'] = $this->db->get('fare')->result();
$this->load->model('time_tbl_model');
$data['flt_nm'] = $this->time_tbl_model->getFlightName();
$this->load->view('front/fare', $data);
}
My View
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th class="center">Fare_ID</th>
<th class="center">Flight Name</th>
<th class="center">Departure_Arrival_Station</th>
<th class="center">Fare</th>
</tr>
</thead>
<tbody>
<?php foreach ($tt as $s): ?>
<tr class="odd gradeX">
<td><?php echo $s->Fare_ID; ?></td>
<td><?php echo $s->Flight_ID; ?></td>
<?php foreach ($flt_nm as $ff): ?>
<td><?php echo $ff->Flight_Name; ?> </td>
<?php endforeach; ?>
<td><?php echo $s->Departure_Arrival_Station; ?></td>
<td><?php echo $s->Fare; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
This is output on the screen, when i executed it.
snag.gy/zLt2d.jpg
I saw your edited post and your image and I believe what you are trying to achieve is like the example of result in the end of this post.
Besides other problems, you're creating some messy stuff when using model code inside controller function. You should separate that, otherwise you're not correctly using MVC pattern.
That said, here's an example of what I would do:
Your model
public function getFlights()
{
$this->db->select('f.*, GROUP_CONCAT(fl.Flight_Name SEPARATOR "<br/>") AS `flight_names`')
->from('fare AS f')
->join('flights AS fl', 'fl.Flight_ID = f.Flight_ID', 'INNER')
->group_by('f.Flight_ID');
return $this->db->get()->result();
}
Your controller
$this->data['flights'] = $this->your_model->getFlights();
Your view
<?php foreach($flights as $flight) { ?>
<td><?php echo $flight->Fare_ID; ?></td>
<td><?php echo $flight->flight_names; ?></td>
<td><?php echo $flight->Departure_Arrival_Station; ?></td>
<td><?php echo $flight->Fare; ?></td>
<?php } ?>
This will reproduce something like:
Fare_ID | Flight Names | Departure Arrival Station | Fare
Flight Names
Flight Names
Fare_ID | Flight Names | Departure Arrival Station | Fare
Fare_ID | Flight Names | Departure Arrival Station | Fare
Flight Names
Flight Names
Flight Names
as i understood
<?php foreach ($tt as $s): ?>
<tr class="odd gradeX">
<td><?php echo $s->Fare_ID; ?></td> //Fare_ID
<?php foreach ($flt_nm as $ff): ?>
<td><?php echo $ff->Flight_Name; ?> </td> //Flight Name
<?php endforeach; ?>
<td><?php echo $s->Departure_Arrival_Station; ?></td>
<td><?php echo $s->Fare; ?></td>
</tr>
<?php endforeach;

Categories