wordpress how to 3 columns post list and columns wrap - wordpress

I am trying to load a WordPress blog list with the following structure.
this is my code
enter image description here
(div class="left-wrap)
post-1
post-4
post-7
(div class="right-wrap)
post-2
post-3
post-5
post-6
(/div)
POST 1 | POST 2 , POST 3
POST 4 | POST 5 , POST 6
POST 7 | POST 8 , POST 7
Thank you for your help.

Populate the post array like the below and separately loop through for the left and right section
<?php
$posts = ["one", "two", "three", "four", "five","six", "seven", "eight", "nine", "ten"];
$left_array = [];
$right_array = [];
foreach($posts as $key => $post){
if(($key+1) % 3 == 1){
$left_array[] = $post;
}
else{
$right_array[] = $post;
}
}
print_r($left_array);
print_r($right_array);
?>
OUTPUT
LEFT ARRAY
Array
(
[0] => one
[1] => four
[2] => seven
[3] => ten
)
RIGHT ARRAY
Array
(
[0] => two
[1] => three
[2] => five
[3] => six
[4] => eight
[5] => nine
)

Related

Calculate price of rental with different periods of pricing wordpress & woocommerce

Hello I'm working on a woocommerce car rental shop and I have the following issue that I'm trying to fix.
Given the following case:
A client comes to rent a car with a date period of October 29 11:00 to November 12 11:00 with a total of 15 days of rentals.
The base price of a car for rent is 17 euros/day with an extra fee of 5 euros/day for October and 17 euros/day with an extra fee of 10 euros/day for November.
So now I have the following dump that shows the same example :
Base Price: 17
Array
(
[10] => Array
(
[fee] => 5.00
[days] => 3
)
[11] => Array
(
[fee] => 10.00
[days] => 12
)
)
When I do the math I'm getting 66 euros for first period 29 October to 31 October and 324 euros for 1 November - 12 November period with a total of 390 euro.
If I were to do an average price 390 / 15 and then set that as price for the car the total would be incorrect.
How could I change the way wocommerce calculates total to calcule the price for the car + other options for rental. ?
Here is a realy basic example
$price = '17.00';
$period_range = array(
'10' => array(
'fee' => 5.00,
'days' => 3
),
'11' => array(
'fee' => 10.00,
'days' => 12
)
);
$charge = array();
foreach($period_range as $range) {
$charge[] = round($range['fee'] * $range['days']);
}
$total = array_sum($charge) + $price;
echo $total;
//this will return 152

Can Hash be sorted by keys or values?

Is it possible to sort Hash by key or by value in following code:
myhash.each_key do |key|
print myhash[key], "\t:\t", key, "\n"
end
Sorting by keys:
myhash = {5 => "five", 3 => "three", 2 => "two", 4 => "four", 1 => "one"}
myhash.keys.sort.each do |key|
print myhash[key], "\t:\t", key, "\n"
end
produces
one : 1
two : 2
three : 3
four : 4
five : 5
Sorting by value is a bit more effort:
myhash.to_a.sort { |item1, item2| item1[1] <=> item2[1] }.each do |item|
puts item.join("\t:\t")
end
produces
5 : five
4 : four
1 : one
3 : three
2 : two
If you want the outcomes in value:key order, change
puts item.join("\t:\t")
to
puts item.reverse.join("\t:\t")
Since Crystal Hash preserves order of insertion, it is possible to actually make a sorted Hash:
myhash.to_a.sort.to_h
To sort by values,
myhash.to_a.sort_by { |k, v| v }.to_h
To sort in-place, it's a bit bulkier:
myhash = {5 => "five", 3 => "three", 2 => "two", 4 => "four", 1 => "one"}
entries = myhash.to_a.sort
myhash.clear
entries.each { |k, v| myhash[k] = v }

get data from 3rd associated table in cakephp 3.x

Ho to do recursion in cakephp 3.x
I have 4 tables
users
----------
|id|name |
|1 | mick|
----------
responses
-------------------------
id|question_id| response|
1 | 1 | slim |
-------------------------
questions
------------------
id | question |
1 | body type |
-------------------
user_respopnses
----------------------------------------------
id | user_id | question_id | response_id |
1 | 1 | 1 | 1 |
----------------------------------------------
following query is auto generated by Cakephp to fetch data
$users = $this->Users->get($id, [
'contain' => ['Responses' ]
]);
Response by running above query
Id Question Id Response Actions
1 1 slim View Edit Delete
I needs to display question attribute(text) instead of question Id but stuck.
Relation defined in User Table
$this->table('users');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsToMany('Responses', [
'foreignKey' => 'user_id',
'targetForeignKey' => 'response_id',
'joinTable' => 'users_responses'
]);
}
after debugging I got this data
'responses' => [
(int) 0 => object(App\Model\Entity\Response) {
'id' => (int) 1,
'question_id' => (int) 1,
'response' => 'slim',
'_joinData' => object(Cake\ORM\Entity) {
'response_id' => (int) 1,
'id' => (int) 1,
'user_id' => (int) 1,
'question_id' => (int) 1,
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'UsersResponses'
},
Actually, User has relation with Responses table and no association with Questions table, but we can get them from 3rd table "users_responses" as this table has question_id, user_id, reponse_id. but cakephp 3.x does not allow recursion. Right now query hitting Response table and users_response table only. Any suggestion or hint to solve this issue much be appreciated.
Following SQL query to achieve result what I want (I used user_id = 4 static value as It was run on mysqlyog )
SELECT users.id , questions.question, responses.response
FROM questions, responses, users_responses , users
WHERE
users.id = 4 AND
users_responses.user_id = 4 AND users_responses.response_id = responses.id
AND
users_responses.question_id = responses.question_id
AND
responses.question_id = questions.id;
"Recursion" isn't quite the right word here, but what you want is
$users = $this->Users->get($id, [
'contain' => ['Responses' => ['Questions']]
]);
This is pretty clearly described in the Eager Loading Associations portion of the manual.

WordPress | WP_Query | Get latest port from category and sub-categories

I have write a theme for WordPress and I like to have the latest posts posted in category and it's sub-categories to be displayed on top of any other post.
An example. Lets say I have the following categories:
Cat 1
Cat 1 - 1
Cat 1 - 2
Cat 1 - 2 - 1
And then I create the following posts :
Post #5 | Cat 1 - 2 | Date 2013
Post #4 | Cat 1 - 1 | Date 2012
Post #3 | Cat 1 - 2 | Date 2011
Post #2 | Cat 1 | Date 2010
Post #1 | Cat 1 - 1 - 2 | Date 2009
In the front end, when I navigate to Cat 1 I do not get as latest post the Post #5 that belong to Cat 1 - 2 where it is sub category of the Cat 1, But instead I am getting the Post #2.
Currently I am using this code :
$categoryID = get_query_var('cat');
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'category__in' => array($categoryID),
'post_status' => 'publish'
);
$eiPost = new WP_Query($args);
The problem is that this code return the latest post only from the top level category and not from the sub categories. How can I modify this code, in order to retrieve the latest posts from all the sub-categories and the top category ?
Is there any solution to this problem ?
'category__in' only displays posts from that category, not children categories.
Try using 'cat' => $categoryID insted. So your $args would be:
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'cat' => $categoryID,
'post_status' => 'publish'
);

listWorksheetNames() is giving incorrect result

$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$worksheet_names = $objReader->listWorksheetNames($input_file);
print_r($worksheet_names);
some times it is printing
Array
(
[0] => test0
)
some times it is printing
Array
(
[0] => test0
[1] => test1
[2] =>test2
)
there are total 3 sheets in my excel file..test0, test1, test2
any idea why it is happening?

Resources