On PeoplesController.php, I wanted to add the Events table by left join, Could someone guide me as i am still learning. Here's the original and working code.
public function constructClasses() {
parent::constructClasses();
$this->loadModel('People');
$this->loadModel('Event');
}
public function view() {
$num_per_page = 8;
if($tab == "images") {
$conditions = array();
$this->paginate['Tagged'] = array(
'tagged',
'model' => 'Image',
'by' => $people['People']['slug'],
'conditions' => $conditions,
'order' => array('Image.created'=>'DESC'),
'limit' => $num_per_page,
'paramType' => 'querystring',
);
$related = $this->paginate('Tagged');
}
}
here's the output:
array(
(int) 0 => array(
'Image' => array(
'object_id' => '1098',
)
),
....
....
(int) 7 => array(
'Image' => array(
'object_id' => '1082',
)
)
)
the goal is to get the slug from the events table by left join
Event.id = Image.object_id
Events table
-----------------------
id | slug
-----------------------
1098 | slug-of-1098
1082 | slug-of-1082
Goal Output:
array(
(int) 0 => array(
'Image' => array(
'object_id' => '1098',
'slug' => 'slug-of-1098'
)
),
....
....
(int) 7 => array(
'Image' => array(
'object_id' => '1082',
'slug' => 'slug-of-1082'
)
)
)
Related
i have an array from database like this. I want to put the array into a menu like this.
Setting
Menu Category
Menu Category View
Group
Group View
Deposits
Group Menu
Group Menu View
Menu
Menu View
ETC
I've been trying But so far every attempt failed
array(
(int) 0 => array(
'MenuCategory' => array(
'name' => 'Setting'
),
'Menu' => array(
'name' => 'Menu Category',
'url' => '/menu_categories/adding',
'parent_id' => null
),
'Site' => array(
'id' => (int) 1
),
'Sub' => array(
'name' => 'Menu Category View',
'url' => '/menu_categories/view'
)
),
(int) 1 => array(
'MenuCategory' => array(
'name' => 'Setting'
),
'Menu' => array(
'name' => 'Group',
'url' => '/groups/adding',
'parent_id' => null
),
'Site' => array(
'id' => (int) 1
),
'Sub' => array(
'name' => 'Group View',
'url' => '/groups/view'
)
),
(int) 2 => array(
'MenuCategory' => array(
'name' => 'Setting'
),
'Menu' => array(
'name' => 'Deposits',
'url' => '/Deposits/requestTiket',
'parent_id' => null
),
'Site' => array(
'id' => (int) 1
),
'Sub' => array(
'name' => null,
'url' => null
)
),
(int) 3 => array(
'MenuCategory' => array(
'name' => 'Setting'
),
'Menu' => array(
'name' => 'Group Menus',
'url' => '/group_menus/adding',
'parent_id' => null
),
'Site' => array(
'id' => (int) 1
),
'Sub' => array(
'name' => 'Group View',
'url' => '/group_menus/view'
)
),
(int) 4 => array(
'MenuCategory' => array(
'name' => 'Setting'
),
'Menu' => array(
'name' => 'Menu',
'url' => '/menus/adding',
'parent_id' => null
),
'Site' => array(
'id' => (int) 1
),
'Sub' => array(
'name' => 'Menu View',
'url' => '/menus/view'
)
)
);
can anyone help me ? ?
You can simply loop through it with three nested foreach() loops, like so:
$arr = // your array;
echo "<ul>";
foreach($arr as $setting => $nested) {
echo "<li>$setting<ul>";
foreach($nested as $category => $nested2) {
echo "<li>$category</li><ul>";
foreach($nested2 as $view) {
echo "<li>$view</li>";
}
echo "</ul></li>";
}
echo "</ul></li>";
}
echo "</ul>";
The only problem with this is that it might print out unnecessary (empty) <ul>s, and that the <ul>s default to different bullet types (easy to fix with CSS though).
Test it out at PHP Sandbox or at Ideone. Here is the output provided:
<ul><li>setting<ul><li>menu category</li><ul><li>menu category view</li></ul></li><li>group</li><ul><li>group view</li></ul></li><li>deposits</li><ul></ul></li><li>group menu</li><ul><li>group menu array</li></ul></li><li>menu</li><ul><li>menu view</li></ul></li></ul></li></ul>
import java.util.Scanner;
public class ARRAY123 {
static int Yaki2=0;
static String ff, ll;
static Scanner Yaki = new Scanner (System.in);
public static void main (String[]a){
//String [] Names = new Array (5);
char menuInput;
char retry;
String [] names;
names = new String [5];
names [0] = "";
names [1] = "";
names [2] = "";
names [3] = "";
names [4] = "";
do {
System.out.println("Names in the Array");
System.out.println("Choose what you want to do with the Array:");
System.out.println("[A]ADD");
System.out.println("[B]EDIT");
System.out.println("[C]DELETE");
System.out.println("[D]SORT");
System.out.println("[E]SEARCH");
menuInput = Yaki.next().charAt(0);
switch (menuInput) {
case 'a':
case 'A':
System.out.println("Add Elements to the Array");
System.out.print("Input whatever element you want to add:");
add(names);
break;
case 'b':
case 'B':
System.out.println("Edit an Element in the Array");
ll = Yaki.next();
System.out.println("Palitan mo na");
ff = Yaki.next();
wait
(ll,ff);
break;
}
System.out.println("Would you like to do more?");
retry = Yaki.next().charAt(0);
}while (retry == 'Y'|| retry == 'y');
System.out.println(names[0]);
System.out.println(names[1]);
System.out.println(names[2]);
System.out.println(names[3]);
System.out.println(names[4]);
}
private static void wait(String ll2, String ff2) {
// TODO Auto-generated method stub
}
static void add (String a[]) {
if (Yaki2 < a.length) {
a[Yaki2] = Yaki.next();
Yaki2++;}
else
System.out.println("The Elements in the Array are complete, you can no longer add more");
}
}
I am a novice in cake and I'm having trouble with a query I want to do in a tables.
$options['joins'] = array(
array('table'=>'users_views',
'alias' => 'UserView',
'type' => 'inner',
'conditions' => array(
'View.id = UserView.view_id',
'UserView.user_id' => $user_id,
'UserView.like' => 1
),
)
);
$options['order'] = array('View.created' => 'desc');
$options['limit'] = 10;
That's my query so far, but what I need now add an OR condition. In this condition OR need to add
'UserView.user_id' => $user_id,
'UserView.like' => 0
I need the query returns the data that matches or are mine and the LIKE value is 0 if it is very difficult to understand please tell me.
Any help or advice will be well received.
Build the conditions like -
'conditions' => array(
0 => array('View.id = UserView.view_id'),
1 => array('UserView.user_id' => $user_id),
3 => 'UserView.like => 1 OR UserView.like => 0'
),
Or with using OR -
'conditions' => array(
0 => array('View.id = UserView.view_id'),
1 => array('UserView.user_id' => $user_id),
2 => 'UserView.like IN (0, 1)'
),
The following should work:
$options['joins'] = array(
array('table'=>'users_views',
'alias' => 'UserView',
'type' => 'inner',
'conditions' => array(
'View.id = UserView.view_id',
'OR' => array(
'UserView.user_id' => $user_id,
'UserView.like' => 1
)
),
)
);
$options['order'] = array('View.created' => 'desc');
$options['limit'] = 10;
Hello I m creating simple blog site here view blog at landing page but if user choose any category the blog under this category only listed . My problem is when user select category from right sidebar it return all table records.
When i print the query it return :
array(
'log' => array(
(int) 0 => array(
'query' => 'SELECT `Category`.`category_id`, `Category`.`name` FROM `cakeBlog`.`categories` AS `Category` WHERE 1 = 1',
'params' => array(),
'affected' => (int) 13,
'numRows' => (int) 13,
'took' => (float) 0
),
(int) 1 => array(
'query' => 'SELECT `Post`.`id`, `Post`.`category_id`, `Post`.`title`, `Post`.`body`, `Post`.`created`, `Post`.`modified`, `Post`.`user_id` FROM `cakeBlog`.`posts` AS `Post` WHERE 1 = 1 LIMIT 20',
'params' => array(),
'affected' => (int) 13,
'numRows' => (int) 13,
'took' => (float) 0
)
),
'count' => (int) 2,
'time' => (float) 0
)
Here is my controller file
PostsController.php
In index function I will check whether category id exist or not?
Please help me...
Thanks
Use the following block where you want to fetch categories
if(is_null($categoryId)) {
$this->Paginator->setting = array(
'limit' =>'10',
'order' => array('Post.id' =>'Desc')
/*'conditions' => array(
'Post.user_id' => AuthComponent::user(id)
)*/
);
$arrPosts = $this->Paginator->paginate('Post');
$this->set('posts', $arrPosts);
} else {
$condition = array('Post.category_id' => $categoryId,'limit' =>'10');
$da = $this->Paginator->setting = array(
'conditions' => $condition
/*'conditions' => array(
'Post.user_id' => AuthComponent::user(id)
)*/
);
$arrPosts = $this->Paginator->paginate('Post');
$log = $this->Post->getDataSource()->getLog(false, false); debug($log);
$this->set('posts', $arrPosts);
}
I've been trying to figure out how to add some computed fields to a model in CakePHP. I'm able to achieve the desired result with the following query:
SELECT project_id,
COUNT(*) AS clicks_total,
SUM(CASE WHEN clicks.redirect_url = projects.url THEN 1 ELSE 0 END) AS clicks_redirected,
SUM(CASE WHEN clicks.redirect_url = projects.url THEN 0 ELSE 1 END) AS clicks_not_redirected
FROM clicks
LEFT JOIN projects ON clicks.project_id = projects.id
GROUP BY project_id
If I attempt to execute it as a custom query Cake transforms the result in such a way that it would require much array manipulation to be usable. I tried to do it the Cake way with the following code, but for some reason the calculated fields end up in a separate array which causes strange behavior in the view:
$this->paginate = array(
'Project' => array(
'fields' => array(
'id', 'name', 'url', 'url_mac', 'url_mobile',
'COUNT(*) AS clicks_total',
'SUM(CASE WHEN Click.redirect_url = Project.url THEN 1 ELSE 0 END) AS clicks_redirected',
'SUM(CASE WHEN Click.redirect_url = Project.url THEN 0 ELSE 1 END) AS clicks_not_redirected'
),
'joins' => array(
array(
'table' => 'clicks',
'alias' => 'Click',
'type' => 'LEFT',
'conditions' => array('Click.project_id = Project.id')
)
),
'group' => 'project_id'
));
$this->set('projects', $this->paginate());
Produces the following result:
array(
(int) 0 => array(
'Project' => array(
'id' => '508705c8-126c-48f9-bd9a-6d79d13bb9ea',
'name' => 'Test Project',
'url' => 'http://www.test.com',
'url_mac' => 'http://www.mac.com',
'url_mobile' => 'http://www.mobile.com'
),
(int) 0 => array(
'clicks_total' => '80',
'clicks_redirected' => '35',
'clicks_not_redirected' => '45'
)
),
(int) 1 => array(
'Project' => array(
'id' => '508b1073-2aa8-4895-b8d9-152ed13bb9ea',
'name' => 'Another Project',
'url' => 'http://another.com',
'url_mac' => 'http://anothermac.com',
'url_mobile' => 'http://anothermobile.com'
),
(int) 0 => array(
'clicks_total' => '134',
'clicks_redirected' => '70',
'clicks_not_redirected' => '64'
)
)
)
Does anyone have any ideas for getting the calculated click counts to show up under the Project array?
You can add virtual fields to your model:
Check it out: http://book.cakephp.org/1.3/view/1608/Virtual-fields
In Project model:
public $virtualFields = array('clicks_total' => 'COUNT(*)');
I am working on a web application and would like to improve my code a little bit.
This is the SQL table structure I made for the application:
Now my question: Is there an easy way in PHP to select (with join or whatever?) the information from one specific event_id on all tables and save them in an array with followed structure for example:
$events = array(
'event_id' => 1,
'event_name' => '{"de":"Weltwirtschaftsforum","fr":"Forum \u00e9conomique mondial","it":"Forum Economico Mondiale"}',
'event_description' => '{"de":"Description DE","fr":"Description FR","it":"Description IT"}',
'event_lastedit' => 2011-12-01 10:23:35,
'locations' => array(
array(
'location_id' => 1,
'location_name' => 'Bern',
'location_date' => '01.01.2012',
'location_deadline' => 1323340607,
'timestamps' => array(
array(
'timestamp_id' => 1,
'timestamp_time' => '10:30',
'timestamp_seats' => 30
),
array(
'timestamp_id' => 2,
'timestamp_time' => '16:30',
'timestamp_seats' => 40
)
)
),
array(
'location_id' => 2,
'location_name' => 'Davos',
'location_date' => '02.02.2012',
'location_deadline' => 1323340607,
'timestamps' => array(
array(
'timestamp_id' => 3,
'timestamp_time' => '12:30',
'timestamp_seats' => 50
),
array(
'timestamp_id' => 4,
'timestamp_time' => '15:30',
'timestamp_seats' => 60
)
)
)
)
);
I hope the question is clear enough.
Greetings
Spinne
Edit:
What i did so far:
$event = $event_db->querySingle("SELECT * FROM rf_events WHERE event_id={$event_id}", true)
$rf_locations = $event_db->query("SELECT * FROM rf_locations WHERE event_id={$event_id}");
$locations = array();
$timestamps = array();
$counter = 0;
// Loop sql query result and save entries in $events array.
while ( $row = $rf_locations->fetchArray() ){
$locations[$counter] = array(
'location_id' => $row['location_id'],
'location_name' => json_decode($row['location_name'], true),
'location_date' => $row['location_date'],
'location_deadline' => $row['location_deadline']
);
$rf_timestamps = $event_db->query("SELECT * FROM rf_timestamps WHERE location_id={$row['location_id']}");
$counter2 = 0;
while ( $row2 = $rf_timestamps->fetchArray() ){
$locations[$counter]['timestamps'][$counter2] = array(
'timestamp_id' => $row2['timestamp_id'],
'timestamp_time' => $row2['timestamp_time'],
'timestamp_seats' => $row2['timestamp_seats']
);
$counter2++;
}
$counter++;
}
SELECT * FROM rf_events, rf_locations, rf_timestamps WHERE
rf_locations.event_id = rf_events.event_id AND
rf_timestamps.location_id = rf_locations.event_id
Then, use add the data into php accordingly.
You can refer to: MYSQL JOIN