Auto increment the alphanumeric characters Id [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I want to auto increment the alphanumeric character's ID and i want to savi it in to my database.
For example:
Example-001
Example-002
Example-003
Example-004
Example-005

You don't really want to store it like that -- bad idea.
Instead, just have your ID INT AUTO_INCREMENT in your MySQL and do something like
<?php echo "$dbRow['name']."-".$dbRow['id'];
Or, if you're OCD -
while(strlen($dbRow['id'] < 3) {
$dbRow['id'] = "0".$dbRow['id'];
}

If you are looking to increment the number at the end of those strings, and you have to do it in PHP, try this:
$str1 = "Example-001";
$parts = explode("-",$str1);
echo sprintf($parts[0] . '-%03d', $parts[1]+1); // Example-002
However I would guess there's a better way, possibly at the database level to accomplish what you need. You would need to explain more and post more code.
Example: http://3v4l.org/V2R5m

I'm not entirely sure what you mean, but since you tagged this as PHP and you're asking how "to auto increment an alphanumeric character's ID," here's a solution:
for ($i=0; $i<=10; $i++){
$number = str_pad($i, 3, "0", STR_PAD_LEFT);
echo "Example-$number <br />";
}
Outputs:
Example-000
Example-001
Example-002
Example-003
Example-004
Etc...
Not only is does it increment the number, but it does it automatically-ish.

Related

How can I increment the value of only one index in the array using PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
$vaccinesArr=array("0","0","0","0","0","0","0","0","0","0","0");
for($i = 0; $i < 11 ; $i++){
while($row = mysqli_fetch_assoc($result)){
if ($row['vaccID']== $i){
$vaccineArr[$i]++;
}
}
}
This is the part of my code, I need to increment according to this condition but I keep on getting syntax error, I searched but all the results I find are using foreach and incrementing all the indexes of the array while here I want to increment only one specific index. Can anyone help?
Your code will only work on the first iteration of the for loop, because when the inner while loop finishes there won't be any more rows to fetch from the query. You could rewind the query, but a better way to do it would be without the for loop.
while ($row = mysqli_fetch_assoc($result)) {
$vaccinesArr[$row['vaccID']]++;
}
Also, if the elements of $vaccinesArr are supposed to be numbers, you shouldn't put them in quotes. PHP will convert the strings to numbers when you use ++, but why be confusing?
$vaccinesArr = array(0, 0, 0, ....);

Split Float & Replace Integer PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have float values in an array... Let's say one of my values is:
5.1234
How do I SWAP the integer in the float. So in the example above, I'd like to swap the 5 with 8. Therefore the new number would be:
8.1234
This needs to be a SWAP, not a mathematical addition as in 5.1234 + 3.
I basically need to split the number in two, the integer (5) and the float value following it (.1234), swap the 5 for the 8 and the recombine them to get 8.1234.
What is the fastest and most elegant way to do this in PHP since I'll be using this on a LOT of data?
To clarify WHY math cannot be used: This is because this is an obj file that's looking for an usemtl library title (Mudbox compliant) from which it extracts the UV space. Then it changes the vert U (or V) accordingly. Problem is these faces may come up more than once. This would make the operation cumulative, which it is NOT. All it needs to do is substituted the integer.
<?php
$number = 5.1234;
$array = explode(".", $number);
// $array[0] contains 5
$newNumber = 8;
$array[0] = $newNumber;
$finalString = $array[0] . '.' . $array[1];
$finalFloat = floatval($finalString); // String to float
echo $finalFloat;
?>
Here is how I would do this. This solution is relevant if you are sure the number will always be formated like followed :
[number].[decimals]
Else you will not be able to always replace the number before the dot.

How to acces the second value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I was wondering if you could give two values ​​to a class and then acces to the second one by POST, something like this (part of the code):
echo "<select name='selecttsk' id='selecttsk'>";
while($w = $bd->obtener_fila($tasker, 0)){
echo "<option class='opcion1' value ='".$w[1]/$w[2]."'>".$w[0]."</option>";
}
echo "</select>";
?>
and then i need to do something like this in other file
$var = $_POST[selecttsk];
but i need $w[2]
thanks
I suppose your $_POST['selecttsk'] does have the values in the following format:
"foo/bar"
You could use "explode" in PHP to get the second part (bar), for example:
$postvar = $_POST['selecttsk'];
$vars = explode("/", $postvar);
// Then
$var = $vars[1]; // Will be the $w[2] from the form
Look into: http://php.net/explode
Beware that if $w[1] or $w[2] ever contains a "/" you might get unexpected results, you could use the limit function of explode to mitigate that issue.
However - I would generally not recommend this workflow.
Why do you need to send two variables with one select?
(could you show us som example of what $w contains)

Convert Range of Numbers to Square Feet [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I am using this function which works fine for a single number entered by user in format 1000 and 1,100
function metersToSquareFeet($meters, $echo = true)
{
$m = $meters;
$valInFeet = $m*10.7639;
$valFeet = (int)$valInFeet;
if($echo == true)
{
echo $valFeet;
} else {
return $valFeet;
}
}
I need to modify for the case where users enter a range of numbers in the format: 1000-2000
The function should be smart enough to convert both numbers and in this case output in this format 10,763-21,527
You're going to need to parse the input yourself.
If you enter in 1000-2000 as a parameter it will think that it's -1000 as it will do the math.
I'm suggesting you change your input to either 2 parameters, an array or a string.
If using a string, use substring to find the int position of '-' http://us1.php.net/manual/en/function.substr.php or use explode like #crayonviolet suggested
Convert both numbers by your constant (10.7639)
Return your desired output as a string or as part of an array
Your choice depends on how your convertToSquareFeet function is used in other parts of your application and the code rework it would have by changing the parameter list.
Hope that helps!

Why php trim the digit before '#' [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I tried to use php rtrim() function to retrive username in the email address, but I met the following problem:
Case 1:
$email = 'merkerxu37#stackoverflow.com';
echo rtrim($email, '#stackoverflow.com');
I got the output:
merkerxu37
Case 2:
$email = 'merkerxu37#37signals.com';
echo rtrim($email, '#37signals.com');
I got the output:
merkerxu
Can anybody tell me why the "37" is missing in Case 2?
That is because you have entered the second parameter '#37signals.com' which consider 3, 7 or 37 as characters to be trimmed.
Reference
trim (and its single-sided variants) take a list of characters to remove. Since 3 and 7 feature in the list you gave, it trimmed them.
Why not just do this?
echo explode("#",$email,2)[0];
// If you don't have PHP 5.4:
// $parts = explode("#",$email,2); echo $parts[0];

Categories