Change the value of variable PHP - php

I have input fields, which I process with AJAX and send it on another file.
The value of the inputs is always different.
How in the file, where I get the data from AJAX, to change the variable id always when I get the data from AJAX.
Example:
I get the data from AJAX in this file:
<?php
$id=1;
echo '<div id="$id"></div>';
?>
Then I display the result in the page with AJAX.
I want the id of the div to be always different.

if you want $id a random number use the function randlink text
$id=rand(0,100); // random between 0 and 100, 0 and 100 incluse
you can also create a random string
function genRandomString($length) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
$string = '';
for ($p = 0; $p < $length; $p++) {
$string .= $characters[rand(0, strlen($characters))];
}
return $string;
}

One can create different random number through time checking, this will create random number on the basis of time and will generate different number each microseconds.
//this will add a char on the beginning of the value so it do not start with numeral
$str=range(a,z);
//using date function for generating random number
$i= $str[rand(0,25)].date('Ymzhisu');

Related

How can you determine remaining space on page with ezpdf?

I'm using rospdf/php-pdf - the middle of the pdf is a table of variable length. I want to make sure there is enough space to print the final section on the page, if not I want to add a new page. I'd like to do something like this but $position has no value. How can I find out the current position?
$position = $pdf->ezSetDy;
if($position < 100){
$pdf->ezNewPage();
}
You can try the following:
$yPos = $pdf->y - $pdf->ez['bottomMargin'];
if ($yPos < 100) {
$pdf->ezNewPage();
}

Advanced Algoithm issues to generate unique 6 char long code

I have used this algorithm to generate 100 codes. Now Once again i have searched the threads on this site and on google before posting. The algorthms found do work in generating the code the issue I got is i have to generate UNIQUE codes from the one i already have. The problem i am finding is all the scripts are all failing to execute.
So basically I have an ARRAY holding 100 voucher codes I need to generate 400 more voucher codes however while my algorithm's works it keeps timing out due to it taking to long to execute. I have tried several other algorithms from the threads on Stack overflow but for some reason they all keep timing out. I need advice on what I can do to my algorithm to actually produce 400 more unique 6 Char codes.
Here is script I have
/**
**Use this portion to get my current 100 codes in an array which i use
**/
$redeem = 'THIS IS A STRING WHICH CREATES AN ARRAY CONTIANING 100 VALUES';
$redeem = preg_replace('/\s+/', '', $redeem);
$redeem_code = str_split($redeem, 6);
/**
**Function generates unique 6 letter voucher codes
**
**/
function random_str($length, $keyspace = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ')
{
$str = '';
$max = mb_strlen($keyspace, '8bit') - 1;
for ($i = 0; $i < $length; ++$i) {
$str .= $keyspace[mt_rand(0, $max)];
}
return $str;
}
/**
**This is the query which checks against my array and then echo's a valid code
**
**/
$redeem_count = count($redeem_code);
$i = 0;
$ia = 1;
while($i <= 500){
$string = random_str(6);
if(in_array($string, $redeem_code))
{
echo $string;
$i= $i+1;
}
}
Constant Error Message on All functions tried.
Fatal error: Maximum execution time of 120 seconds exceeded in C:\wamp\www\nfr_chip.php on line 115
You never add anything to the array and only increase $i if the code already exist.
This should work...
$redeem_count = count($redeem_code);
while($redeem_count <= 500) {
$string = random_str(6);
if(!in_array($string, $redeem_code))
{
$redeem_code[] = $string;
$redeem_count++;
}
}
And for the way you are splitting your string with codes, instead of using preg_replace (avoid regex unless you really need it):
$redeem_code = explode(" ", $redeem);
You can safely use openssl_random_pseudo_bytes with bin2hex to generate pseudo-random, as the name implies, strings.
Here's an example:
for ($i=1;$i<=5;$i++){
print bin2hex(openssl_random_pseudo_bytes(3)) . PHP_EOL;
}
//output
9282fb
3b9798
c187a0
a058e3
df2e4b
3 bytes will give you a 6 char string

How to save user-created content as a URL (hash) parameter?

I want to enable "Save" feature in my web app. I will send data to a PHP post receiver, and save the data in a MySQL row, so normally I can load it with its id like this:
site.com/content/123
But I want (just for the fancy looks) to use a "hash" (not sure if it's the right term) for this, like :
site.com/content/A2w7SqZ
just like jsFiddle does. How can I convert the id (integer) to a hash?
Example :
http://jsfiddle.net/sfu24/
The only way I can think of is MD5. But it generates a very long string. I think 6 characters are more than enough.
So how can I make a hash system like jsfiddle?
Thanks for any help !
P.S. I'm sure this question has been asked million times. But I couldn't find it. If you know an already-existing answer, please post the link and I will delete the question.
to generate random chars
function generate_random($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for($i = 0; $i < $length; $i ++) {
$randomString .= $characters [rand ( 0, strlen ( $characters ) - 1 )];
}
return $randomString;
}
Take a look as base_convert()
or the hashids
which will help you generate short hashes from numbers (like YouTube and Bitly).
function generateHash($int) {
$rand_letters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$newstr = '';
for ($i = 0; $i < strlen($int); $i++) {
$newstr .= $rand_letters[rand(0, strlen($rand_letters)-1)];
}
return $newstr;
}
function myHash($int) {
$newstr = generateHash($int);
$result = $db->query("SELECT COUNT(*) FROM hashes WHERE hash = '$newstr';");
if ($result->num_rows > 0) {
myHash($int);
}
return $newstr;
}
echo myHash(973451);
You generate string on the base of the length of your passed integer. That's what generateHash() does. And myHash() uses this string, if it's already present, runs itself again, until generate not-present string, so returns it.
The hashes are random, so they are not straight reversable (they still can be), but "12345" will not result in one and the same string everytime.

How i generate lotto number using ajax and PHP

I want to generate the lotto number like generating here
http://www.nationallottery.co.za/lotto_home/NumberGenerator.asp
may i know what will be the logic or way to generate the lotto number using PHP,mysql and Ajax.
I will be thankful of you.
Sample Example:
$generated = array();
while (count($generated) < 6)
{
$no = mt_rand(1, 49);
if(!array_search($no, $generated))
{
$generated[] = $no;
}
}
echo implode(" : ", $generated);
You just need to generate random numbers.
Create multiple random numbers and style them however you want them. That site appears to have replaced text numbers with images which are probably programatically coded. If you want multiple rows like they offer, just make a form like they have and return the correct number of rows. Shouldn't be too hard
The php function you are looking for is mt_rand()
Use it to generate an integer between two given values, something like this:
<?php
for($i = 1; $i <= 6; $i++){
echo mt_rand(1,45).' ';
}
?>

PHP code for generating decent-looking coupon codes (mix of letters and numbers)

For an ecommerce site I want to generate a random coupon code that looks better than a randomly generated value. It should be a readable coupon code, all in uppercase with no special characters, only letters (A-Z) and numbers (0-9).
Since people might be reading this out / printing it elsewhere, we need to make this a simple-to-communicate value as well, perhaps 8-10 characters long.
Something like perhaps,
AHS3DJ6BW
B83JS1HSK
(I typed that, so it's not really that random)
$chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$res = "";
for ($i = 0; $i < 10; $i++) {
$res .= $chars[mt_rand(0, strlen($chars)-1)];
}
You can optimize this by preallocating the $res string and caching the result of strlen($chars)-1. This is left as an exercise to the reader, since probably you won't be generating thousands of coupons per second.
Try this:
substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, 10)
Why don't keep it simple?
<?php
echo strtoupper(uniqid());
?>
Always returns 13 character long uppercased random code.
You can use the coupon code generator PHP class file to generate N number of coupons and its customizable, with various options of adding own mask with own prefix and suffix. Simple PHP coupon code generator
Example:
coupon::generate(8); // J5BST6NQ
http://webarto.com/35/php-random-string-generator
Here you go.
function randr($j = 8){
$string = "";
for($i=0;$i < $j;$i++){
srand((double)microtime()*1234567);
$x = mt_rand(0,2);
switch($x){
case 0:$string.= chr(mt_rand(97,122));break;
case 1:$string.= chr(mt_rand(65,90));break;
case 2:$string.= chr(mt_rand(48,57));break;
}
}
return strtoupper($string); //to uppercase
}
If there are no security requirements for these, then you don't really need randomly generated codes. I would just use incremental IDs, such as those generated by whatever RDBMS you use. Optionally, if you have different types of coupons, you could prefix the codes with something, e.g.:
CX00019 QZ0001C
CX0001A QZ0001D
CX0001B QZ0001E
Alternately, you could even use dictionary words in the coupon, as such coupon codes are easier to remember and faster for users to type. Companies like Dreamhost use these for their promo codes, e.g.:
Promo60
NoSetupFee
YELLOWGORILLA82
Some of these are obviously human-created (which you might want to have the option of), but they can also be generated using a dictionary list. But even if they are randomly-generated nonsense phrases, the fact that the characters follow a logical pattern still makes it much more user-friendly than something like R7QZ8A92F1. So I would strongly advise against using the latter type of coupon codes just on the basis that they "look cool". Your customers will thank you.
$size = 12;
$string = strtoupper(substr(md5(time().rand(10000,99999)), 0, $size));
function generateCouponCode($length = 8) {
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$ret = '';
for($i = 0; $i < $length; ++$i) {
$random = str_shuffle($chars);
$ret .= $random[0];
}
return $ret;
}
you can find a lot of function in php rand manual
http://php.net/manual/en/function.rand.php
i like this one
<?php
//To Pull 8 Unique Random Values Out Of AlphaNumeric
//removed number 0, capital o, number 1 and small L
//Total: keys = 32, elements = 33
$characters = array(
"A","B","C","D","E","F","G","H","J","K","L","M",
"N","P","Q","R","S","T","U","V","W","X","Y","Z",
"1","2","3","4","5","6","7","8","9");
//make an "empty container" or array for our keys
$keys = array();
//first count of $keys is empty so "1", remaining count is 1-7 = total 8 times
while(count($keys) < 8) {
//"0" because we use this to FIND ARRAY KEYS which has a 0 value
//"-1" because were only concerned of number of keys which is 32 not 33
//count($characters) = 33
$x = mt_rand(0, count($characters)-1);
if(!in_array($x, $keys)) {
$keys[] = $x;
}
}
foreach($keys as $key){
$random_chars .= $characters[$key];
}
echo $random_chars;
?>
$length = 9;
$code = (strtoupper(substr(md5(time()), 0, $length)));
Just Write
$voucher_no = date('ymd') . rand(1000, 9999);
while(SapItem::where('voucher_no', $voucher_no)->exists()){
$voucher_no = date('ymd') . rand(1000, 9999);
}
Output: 2204171447

Categories