I'm having an issue producing this array multiple times upon how many coupons purchased.
Now it looks like
$coupon_array = array(
'user_id'=>$_POST["user_id"],
'mergent_id'=>$_POST["merchant_id"],
'deals_id'=>$_POST["deal_id"],
'order_id'=>$order_id,
'secret'=>$secret,
'expire_time'=>$time,
'create_time'=>$time,
'status'=>1
);
$this->common_model->insertData('coupon', $coupon_array);
But i have a post value such as:
"quantity"=>$_POST["quantity"]
and i would like to produce this X times. Example:
$quantity x $this->common_model->insertData('coupon', $coupon_array);
Sorry for my english, and i hope i explain this so it's understandable... ;)
Another one! when we insert the coupons they all have the same md5($secret), is it possible to have that also with all the different code...
$secret = md5($secret);
$coupon_array = array(
'user_id'=>$_POST["user_id"],
'mergent_id'=>$_POST["merchant_id"],
'deals_id'=>$_POST["deal_id"],
'order_id'=>$order_id,
'secret'=>$secret,
'expire_time'=>$time,
'create_time'=>$time,
'status'=>1
);
Well, if I understand what you want, you can use for, but that's obvious:
for($i=0; $i<$this->input->post('quantity');$i++) {
$coupon_array['secret'] = md5($coupon_array['secret'].$i);
$this->common_model->insertData('coupon', $coupon_array);
}
Also, never use $_POST["..."] in CodeIgniter, use only $this->input->post('...') as it escapes properly. More info about input class can be found here.
for ($i=0; $i<$quanity; $i++) {
$this->common_model->insertData('coupon', $coupon_array);
}
Related
UPDATE: I think the cakePhp updateAll is the problem. If i uncomment the updateAll and pr the results i get in 1-2 seconds so many language Detections like in 5 minutes!!!! I only must update one row and can determine that row with author and title... is there a better and faster way???
I'm using detectlanguage.com in order to detect all english texts in my sql database. My Database consists of about 500.000 rows. I tried many things to detect the lang of all my texts faster. Now it will take many days... :/
i only send 20% of the text (look at my code)
i tried to copy my function and run the function many times. the copied code shows the function for all texts with a title starting with A
I only can run 6 functions at the same time... (localhost)... i tried a 7th function in a new tab, but
Waiting for available socket....
public function detectLanguageA()
{
set_time_limit(0);
ini_set('max_execution_time', 0);
$mydatas = $this->datas;
$alldatas = $mydatas->find('all')->where(['SUBSTRING(datas.title,1,1) =' => 'A'])->where(['datas.lang =' => '']);
foreach ($alldatas as $row) {
$text = $row->text;
$textLength = round(strlen($text)*0.2);
$text = substr($text,0,$ltextLength);
$title = $row->title;
$author = $row->author;
$languageCode = DetectLanguage::simpleDetect($text);
$mydatas->updateAll(
['lang' => $languageCode], // fields
['author' => $author,'textTitle' => $title]); // conditions*/
}
}
I hope some one has a idea for my problem... Now the language detection for all my texts will take more than one week :/ :/
My computer runs over 20 hours with only little interruptions... But i only detected the language of about 13.000 texts... And in my database are 500.000 texts...
Now i tried sending texts by batch, but its also to slow... I always send 20 texts in one Array and i think thats the maximum...
Is it possible that the cakePhp 3.X updateAll-function makes it so slowly?
THE PROBLEM WAS THE CAKEPHP updateAll
Now i'm using: http://book.cakephp.org/3.0/en/orm/saving-data.html#updating-data with a for loop and all is fast and good
use Cake\ORM\TableRegistry;
$articlesTable = TableRegistry::get('Articles');
for ($i = 1; $i < 460000; $i++) {
$oneArticle = $articlesTable->get($i);
$languageCode = DetectLanguage::simpleDetect($oneArticle->lyrics);
$oneArticle->lang = $languageCode;
$articlesTable->save($oneSong);
}
I've had excellent support here, so I figured I'd try again, as I have no clue where to even begin looking for this answer.
I have a simple MySQL database, named "testimonials", which contains 3 tables, "id", "name", "content"
What I want to do, is display testimonials within a fixed size block. Just simply displaying the content is no problem at all, however where I'm stuck is the (somewhat) unique way I'm trying to make it work. I would like to display a random item on each page load, and then to check the character length of the "content" within the testimonial, and if it's equal to or greater than XX length, then just display the one testimonial, otherwise if it's less than XX in length to display a second testimonial (assuming it combined with the first doesn't break the container box).
The box in question is 362px in width and 353px in height using 14px font with Verdana. An example of how the testimonial will appear on the page is like this:
"This is the testimonial content, some nice message from a client."
-- Billy Bob, Owner, Crazy Joe's Tavern
The "name" table in the database holds everything in bold (minus the -- of course), in case someone felt the need to ask.
As I typed that, I felt as if I was asking for a miracle, however I'll still post the question, hoping someone might just know the answer. As always, thanks for any help I may get, if this is just simply asking too much, I'm not totally against the idea of only displaying one testimonial at a time making a ground rule saying they have to contain a minimum of XX characters.
Thanks!
Quick Update: I didn't expect to get answers so quickly, I'm not at my desk at the moment, so as soon as I sit back down I'll go through and see which answer fits best. However, do you guys get together and try to make your answer more complex than the previous answer? lol, thanks though, for anyone who's offering help, you guys rock!
Final edit: I decided against this whole idea, as it just way over complicated everything. For the time being, I'm just going to display all testimonials, and make them scroll, while I work on a jQuery snippet to make it prettier. Thanks everyone for your help though! Should I decide again to do this, I'll be trying my chosen answer.
You just need a loop. Pseudo-code:
$length = 0;
$target = 200; // or whatever
while( $length < $target ) {
$comment = getOneComment();
displayComment($comment);
$length += strlen( $comment['content'] ); // assuming getOneComment() returns an associative array
}
To make it pretty, if the display box is going to a be a fixed height, you could use some jQuery to toggle whether to show the second comment on not.
Assuming you have testimonials in an array:
$testimonials = array(
't1' => array(
'content' => 'testimonials 1 content..',
'author' => 'the author'
),
't2' => array(
'content' => 'testimonials 2 content..',
'author' => 'the author 2'
),
);
You could have an maxLengthTestimonialsContent and maxLenthAllTestimonnials variable :
$maxLengthTestimonialsContent = 120;
$maxLenthAllTestimonnials = 240;
And now with a simple loop you build the array testimonials that you will use to show:
$testimonialsToShow = array();
$i = 1; $totalLength = 0
foreach($testimonials as $t) {
if( $i > 1 && strlen( $t['content']) < $maxLengthTestimonialsContent
&& $totalLength < $maxLenthAllTestimonnials )
break; // basically here you test that testimonials less first
// and with less length than maxLengthTestimonial, and also
// total length less than maxLengthAll to be stored
//in $testimonialsToShow
else {
$testimonialsToShow[] = $t;
$totalLength = $t['content'];
}
}
Something like this is what you would need.
<?php
$str = $res["testimonial"];
if (strlen($str) > 50) {
// Logic to retrieve and display second testimonial
}
?>
Obviously there's some more processing you'll have to come up with to determine if the second testimonial is short enough to fit or not. But that should get you started.
EDIT:
For the randomization, I use this on my own site:
$referrals = mysql_query("SELECT id FROM ts_testimonials");
$referralView = array();
$i = 0;
while ($newReferral = mysql_fetch_array($referrals)) {
$referralView[$i] = $newReferral['id'];
$i++;
}
if (sizeof($referralView) >= 1){
$referralTop = rand(0,sizeof($referralView)-1);
$newReferralTop = mysql_fetch_array(mysql_query("SELECT * FROM ts_testimonials WHERE id = '".$referralView[$referralTop]."'"));
if (sizeof($referralView) >=2){
$referralBottom = rand(0,sizeof($referralView)-1);
while ($referralBottom == $referralTop) {
$referralBottom = rand(0,sizeof($referralView)-1);
}
$newReferralBottom = mysql_fetch_array(mysql_query("SELECT * FROM ts_testimonials WHERE id = '".$referralView[$referralBottom]."'"));
}
}
I'm not especially experienced in PHP, but I've been trying to create a basic blog for a friend's website. I thought the easiest thing to do for now would be to use static files, so I'm using XML to store the blog entries. I've managed to set it up perfectly in that I can display the posts as I want them. However, I now want a nav bar which will allow me to select posts based on date, as most blogs have. The files are simply named 1.xml, 2.xml, 3.xml etc. so I can iterate through them. Here's the code that shows how the data array is organised (it's an array within an array so that the first level will be equivalent to the number in the filename +1). So I'm having a lot of trouble working out how I can create the nav bar (ul, li etc.) from this data. Presumably I'd need years to be unique and then the months in the years to be unique and also with the days, then I can have each title (obviously a link) come under the proper date.
$data = array();
for ($i = 1; $i <= $numberOfPosts; $i++) {
$filename = './blogentries/' . $i . '.xml';
if (!file_exists($filename))
throw new Exception();
$blogentry = simplexml_load_file($filename);
$title = $blogentry->title;
$dateD = $blogentry->date->day;
$dateM = $blogentry->date->month;
$dateY = $blogentry->date->year;
if (strlen($dateY) === 2) $dateY = '20' . $dateY;
$entryParagraphs = $blogentry->entry->children();
$data[] = array(
(string)$title,
array(
(string)$dateY,
(string)$dateM,
(string)$dateD
),
$entryParagraphs
);
}
Thanks for any help you can give. And sorry if I've not been as eloquent as I might have been, I hope you'll forgive my relative ignorance!
From what I understand, I will go with this type of solution :
First of all, if you do know a little bit of OOP, please create an Article class.
After that, here is what I would do for what you are asking :
Instead of creating an array (which should in fact be a class, (the first array, I don't know if you do realize that), I would do this array :
$data[$dateY][$dateM][$dateD]=$blogentry;
Then, you have all your articles classified by Year, then month, then day, so it becomes really simple to end with your request.
edit :
When I said it should be a class, I'm talking about this array :
array(
(string)$title,
array(
(string)$dateY,
(string)$dateM,
(string)$dateD
),
$entryParagraphs
)
It's typically what a class is designed for.
Hey there, I'm a total newbie and I'm looking for a way to have a search box with autocomplete just like google does.
I've searched and the best prospect that I've found seems to be http://www.pengoworks.com/workshop/jquery/autocomplete.htm which I found on a forum. The guy who suggested it says he uses it with http://code.google.com/p/searchable-behaviour-for-cakephp/ which is dead on because I've managed to install searchable on my last attempt at figuring out cakephp.
The thing is, I've not used much javascript before and I'm a bit confused as to what exactly I'm meant to be doing. The documentation with the autocomplete codes doesn't go into any detail that I can understand.
If we assume that I manage to get searchable behaviour installed correctly, could any kind person explain to me how I would go about making the autocomplete work?
It says to just use:
$("selector").autocomplete(url [, options]);
eg:
$("#input_box").autocomplete("autocomplete_ajax.cfm");
Autocomplete expects an input element with the id "input_box" to exist. When a user starts typing in the input box, the autocompleter will request autocomplete_ajax.cfm with a GET parameter named q
thats the bit I don't get. Where am I meant to put that? And once I've put it somewhere then do I just need to name one of my inputs "input_box"?
thanks in advance.
There are three steps:
1) create a totally normal form with an input field, using the Html helper in your view:
// app/views/foo_bars/search.ctp
<?php
echo $this->Form->create('FooBar');
echo $this->Form->input('field');
echo $this->Form->end('Submit');
?>
2) Have a jquery autocomplete fired:
// app/views/foo_bars/search.ctp
<?php
echo $this->Html->scriptBlock(
.'$("#FooBarField").autocomplete({'
.'source:"/foo_bars/find",'
.'delay: 100,'
.'select:function(event,ui){$(this).parent().parent().children("input").val(ui.item.id);},'
.'open:function(event,ui){$(this).parent().parent().children("input").val(0);}'
.'});'
array('inline' => false));
?>
3) Query a database through a controller to get possible values:
// app/controllers/foo_bars_controller.php
<?php
public function find() {
if ($this->RequestHandler->isAjax()) {
$this->autoLayout = false;
$this->autoRender = false;
$this->FooBar->recursive = -1;
$results = $this->FooBar->find('all', array('fields' => array('id', 'name'), 'conditions' => array('name LIKE "%'.$_GET['term'].'%"')));
$response = array();
$i = 0;
foreach($results as $result){
$response[$i]['value'] = $result['FooBar']['name'];
$response[$i]['id'] = $result['FooBar']['id'];
$i++;
}
echo json_encode($response);
}
}
?>
echo $this->Html->scriptBlock(
'$("#FooBarField").autocomplete({'
.'source:"/Search/find",'
.'delay: 100,'
.'select:function(event,ui){$(this).parent().parent().children("input").val(ui.item.id);},'
.'open:function(event,ui){$(this).parent().parent().children("input").val(0);}'
.'});'.
array('inline' => false));
In some part of my code I need something like this:
$product_type = $product->type;
$price_field = 'field_'.$product_type.'_price';
$price = $product->$$price_field;
In other words I need kind of KVC - means get object field by field name produced at the runtime.
I simply need to extend some existing system and keep field naming convention so do not advice me to change field names instead.
I know something like this works for arrays, when you could easily do that by:
$price = $product[$price_field_key].
So I can produce key for array dynamically.
But how to do that for objects?
Please help me as google gives me river of results for arrays, etc...
Thank you
$price = $product->{$price_field};
Sorry Guys.
It was much easier than I thought.
Hopefullty it will help someone. Simply put:
$price_field = 'field_'.$product_type.'_price';
$price = $product->$price_field;
So you can use varialbe to get object field in Php.
I went to far with those $$ ;-)
Regards
How about using get_object_vars?
$price_field = 'field_'.$product_type.'_price';
$instvars = get_object_vars($product);
$price = $instvars[$price_field];
Actualy it would work as follows.
$product_type = $product->type;
$price_field = "field_".$product_type"._price";
$price = $product->$price_field;