I have database data like text [mydata] some text [otherdata] some more text and I want to replace those [mydata] , [otherdata] with dynamic info such as category title for instance.
So the result of my data would be like:
text Category Title some text Category Desc some more text
instead of
text [mydata] some text [otherdata] some more text
I think that can be happen by preg_replace but not quite sure.
code
View::composer('*', function ($view) {
$notes = Notes::all();
foreach($notes as $note){
$descrip= $note->description;
}
$view->with('descrip', $descrip);
});
More
So basically $note->description content is this data:
text [mydata] some text [otherdata] some more text
I want to replace those elements by data from categories table.
Any idea?
Update
well i was digging and get code below (useing str_replace) however it has some issues,
View::composer('*', function ($view) {
//getting categories and pull out the values i want to use as `[....]`
$catC = Category::all();
foreach($catC as $catD){
$meta_title = $catD->title;
$meta_desc = $catD->meta_description;
$meta_tags = $catD->meta_tags;
}
//replace those categories values with element in database `[......]`
$seotemplates = SeoTemplate::all();
foreach($seotemplates as $seotemplate){
$myCustom = str_replace_array('[cattitle]', [$meta_title, $meta_desc, $meta_tags], $seotemplate->categories_desc_template);
}
$view->with('myCustom', $myCustom);
});
Issues
I can only get [cattitle] but what about [catmeta] & [cattags]?
$meta_title will always return first value from categories table (for example if i'm visiting HP category page it return ACER as [cattitle] or any other category pages i visit. It's always ACER
Update 2
I solved the issue 1 of my first update but still issue 2 remained, here is updated code
View::composer('*', function ($view) {
//categories
$catC = Category::all();
$catD = [];
foreach($catC as $catD){
$catD = [
$cattitle = $catD->title,
$catmeta = $catD->meta_tags,
$catdesc = $catD->meta_description
];
}
$a1 = array("[cattitle]","[catmeta]","[catdesc]");
$seotemplates = SeoTemplate::all();
foreach($seotemplates as $seotemplate){
$myCustom = str_replace($a1, $catD, $seotemplate->categories_desc_template);
}
$view->with('myCustom', $myCustom);
});
Current issue:
$meta_title will always return first value from categories table (for example if i'm visiting HP category page it return ACER as
[cattitle] or any other category pages i visit. It's always ACER
I see a couple of issues with your code that I think will help with getting the solution that you are after.
First like mentioned by #hktang said in his comment you are duplicating your variables and then assigning it's value over and over again instead of adding to it. Try this:
$catE = [];
foreach($catC as $catD){
$catE[] = [
$cattitle = $catD->title,
$catmeta = $catD->meta_tags,
$catdesc = $catD->meta_description
];
}
Second you are also resetting the value of $myCustom with each loop of the for each. Try this:
$myCustom = []
foreach($seotemplates as $seotemplate){
$myCustom[] = str_replace($a1, $catE, $seotemplate->categories_desc_template);
}
This should result in you getting an array of seotemplates with the values replaced but I have not testing this.
Related
So I have a Post which has Comments -- I'm trying to get the Comment # position within that post. For example, a Post has 15 comments, I want to be able to get the numerical position (i.e 1 (first post), 2 (second post), 3 (third post), etc, etc), and put this into a function somehow. That way when I can call $comment->position() and it will show as the 4th, 5th, whatever position in it is in.
I've done some searching around the web and couldn't find a solution. Any help is greatly appreciated! This is what I have so far:
public function position($id,$arr)
{
$total = $this->post->comments->count();
$position = $this->pluck('post_id')->search($this->id) + 1;
return ceil($total / $position);
//$comments_per_page = 25;
//$pos = array_search($id,$arr);
//$pos = $pos+1;
//return ceil($pos/$comments_per_page);
}
You should first get all your comments as collection.
// all comments as collection
$comments = $this->post->comments;
Then you can search through the collection using the search function and inserting an id you want to search for ... or any other param you want.
$id = 2;
$commentIndex = $comments->search(function($comment) use ($id) {
return $comment->id === $id;
});
I'm getting data through the 2 table in form of array (like address) and one to find particular pin code in another table here is the 2 model that i'm using to find and match the result and perform the tax function eg. if one table have the array a.b,c,d the second table have the value to find the value store in it eg. if a = 1 if b=2 like that here is my code idea trying to implement but no success
try
{
$toltax = toltax::wheresource('LIKE','%'.$s_address.'%')->get();
$tsource = $toltax->source;
$tdestination = $toltax->destination;
if(!empty($toltax = toltax::where($s_address, 'LIKE' ,"%$tsource%")
->where($d_address,'LIKE',"%$tdestination%")
->get('tax')
)
){
Log::info("toll tax".$toltax->tax);
$Tax = $Tax + $toltax->tax;
}
}catch(ModelNotFoundException $e) {
return false;
}
try this i hope help you
$tax = DB::table('toltax as latest')
->whereSource('LIKE','%'.$s_address.'%')
->whereNotExists(function ($query) {
$query->select(DB::raw(1))
->from('toltax')
->whereRaw('source LIKE latest.source')
->whereRaw('destination LIKE latest.destination');
})
->get(['tax']);
I try to make a "getThreeWorks" method with Laravel. Each view post to its 'orientation'. It is stored in the same table. For example, "Work 1 has the web orientation, Work 2 has the 2D orientation and Work 3 has the 3D orientation".
At the end of each post, I would like to propose a link to 3 other works (in a random order).
So I would like a link to a work that has the "web" orientation, another that has the "2D" orientation and one that has the "3D" orientation.
I can't get SQL query at all. Can you help me ? Thank you !
public function getThreeWorks()
{
$workFrom3D = Work::where('orientation', '3D')->inRandomOrder->limit(1)->get();
$workFrom2D = Work::where('orientation', '2D')->inRandomOrder->limit(1)->get();
$workFromWeb = Work::where('orientation', 'web')->inRandomOrder->limit(1)->get();
}
Can't you remove the limit(1) and instead use a group by orientation? Then you get one of each.
You can create a new collection with your current code.
$work = collect([$workFrom3D, $workFrom2D, $workFromWeb]);
If i understood, you want show some "posts" on footer of each post, right ? you can try this:
public function getThreeWorks()
{
$workFrom3D = Work::where('orientation', '3D')->get();
$workFrom2D = Work::where('orientation', '2D')->get();
$workFromWeb = Work::where('orientation', 'web')->get();
$randomFrom3d = $workFrom3D->random();
$workFrom2D = $workFrom2D->random();
$workFromWeb = $workFromWeb->random();
}
also, you should include one more condition on your where, to avoid repeat the same post on page where will show this list, like:
$workFromWeb = Work::where('orientation', 'web')
->where('post_id','!=', $actualPostId)->get();
I'm working on a PHP project and would love to get some help on variables.
I have a function that looks like this:
function custom_get_tour_nearest_booking_dates( $tour_id, $limit = 2, $exclude_booked_tickets = false ) {
$result = array();
if ( $tour_id < 1 ) {
return $result;
}
The database holds a set of products, each of these with a unique ID made up by numeric values, like 1233,2355,6532 and so on.
If I want to display the characteristics of one of these products, how do I link the ID to the variable $tour_id?
Somewhere you call that method: custom_get_tour_nearest_booking_dates(4);
Like the example above, 4 is the tour_id. Besides that, your method is kinda useless.
I want to apply search filters in my project. I have options tables where options are being saved with the option values with parent id of option id. For example brand is saving as option with parent id set to 0 and all brands have brand id as their parent id set and while saving product I am saving product options in product_options table. Now i want to apply filters in product listing page. I am using following code for filtration:
$conditions = array();
$product_options = $this->ProductOption->find('list',array('fields'=>array('product_id'),'conditions'=>array('ProductOption.option_value_id'=>$data['data']['options'])));
$conditions = array_merge($conditions,array('Product.id'=>array_unique($product_options)));
$prod_info = $this->paginate('Product',$conditions);
$this->set(compact('prod_info'));
When I search any product with their brand name it works fine but if I try to search with the price (also an option) then it gives other brand products also which have price equal to filter price option. Please check following link to understand problem correctly.
http://primemart.in/Food-Processors-Ii4zRGAKYAo=
Please anyone help me to come out my problem.
Thanks.
Please have a look on my code which I used to pass conditions in and to get results
$product_options = $this->ProductOption->find('list',array(
'fields'=>array('product_id'),
'conditions'=>array('ProductOption.option_value_id'=>$data['data']['options'])
));
//$this->Option->unBindModel(array('belongsTo'=>'Product'));
$product_options = $this->Option->find('all', array(
'conditions'=>array('Option.id'=>$data['data']['options'])
));
//pr($product_options);
$opt_arr = array();
foreach ($product_options as $op) {
$opt_arr[$op['Option']['parent_id']][] = $op['Option']['id'];
}
$conditions_arr = array();
foreach($opt_arr as $opt) {
$key_arr = array();
foreach($opt as $op) {
$key_arr['OR']['ProductOption.option_value_id'][] = $op;
}
$conditions_arr['AND'][] = $key_arr;
}
$pr_options = $this->ProductOption->find('list', array(
'conditions'=>$conditions_arr,
'fields'=>array('product_id')
));
$conditions = array_merge($conditions, array('Product.id'=>array_unique($pr_options)));
I would try code bellow. I assume that $conditions constist of the other conditions you mention in your question.
$conditions = ... // other conditions you mentioned
$conditions = array('AND'=>array($conditions, array('Product.id'=>array_unique($product_options))));
$prod_info = $this->paginate('Product',$conditions);