Can If Statment created based on loop? - php

For example
$InitArray[1] = 4;
$InitArray[2] = 5;
$InitArray[3] = 6;
I want to make if statment with that syntax
if($Type = /* key */){
$Position = /* value */
}
which means
if($Type = 1){
$Position = 4;
}
if($Type = 2){
$Position = 5;
}
if($Type = 3){
$Position = 6;
}
How to make that if statment through array loop?
Edit :
Array isn't static , User put his own array
,It Could be like that for example
$InitArray[4] = 34;
$InitArray[5] = 13;
or
$InitArray[6] = 12;
$InitArray[13] = 84;
$InitArray[52] = 23;
$InitArray[78] = 10;
what I mean is something like this
foreach($InitArray as $TypeWritten => $PositionWritten){
if($Type = $TypeWritten){
$Position = $PositionWritten;
}
}

The Equal comparison operator in php is double equal sign ==. So you need to at least change all if statement to use == instead of =.
foreach($InitArray as $TypeWritten => $PositionWritten){
if($Type == $TypeWritten){
$Position = $PositionWritten;
// some code to use the $Position variable ...
// ...
}
}

You're just missing the correct comparison operator. Just change the = into == (or === if the variables you are comparing are of the same type),
foreach($InitArray as $TypeWritten => $PositionWritten){
if($Type == $TypeWritten){
$Position = $PositionWritten;
}
}
Hope this helps,

Related

How do I query incomplete input

I want to transform query string like pending, shipped or cancel to number status.
$q = strtolower($keyword);
if($q == 'pen' or $q == 'pend' or $q == 'pending' ) {
$d = 1;
} elseif($q == 'shi' or $q == 'ship' or $q == 'shipped') {
$d = 2;
} elseif($q == 'can' or $q == 'cancel' ) {
$d = 3;
} else {
$d = 4;
}
$query->whereStatus($d);
Current query working fine but too much or. It's possible to do in shorten way?
str_is(query, stringToSearch) will probably be enough:
if (str_is('pen*', $q)) {
$d = 1;
}
Else you could parse them from arrays:
$pendingArray = ['pen', 'pend', 'pending'];
if (in_array($q, $pendingArray)) {
$d = 1;
}
If these are all the conditions you need, you could always use a switch.
$q = strtolower($keyword);
$d = 4;
switch($q) {
case 'pen':
case 'pend':
case 'pending':
case 'pen':
$d = 1;
break;
case 'shi':
case 'ship':
case 'shipped':
$d = 2;
break;
case 'can':
case 'cancel':
$d = 3;
break;
}
$query->whereStatus($d);
If this needs to be called on a model, it could be saved to a Laravel scope function like so:
on Laravel model
public function scopeSearchStatus($query, $keyword) {
/** All the code above **/
}
Then it can be called cleanly anywhere you'd like:
SomeModel::searchStatus($keyword);
You could also try this:
<?php
$q = strtolower($keyword);
$d = (preg_match('/\b(pen|pend|pending)\b/', $q)) ? 1 : 4;
$d = (preg_match('/\b(shi|ship|shipped)\b/', $q)) ? 2 : 4;
$d = (preg_match('/\b(can|cancel|)\b/', $q)) ? 3 : 4;
$query->whereStatus($d);

Add unique identifier to first iteration in PHP for loop

I am using a for loop on my client's website to insert purchased ticket information into a database. The loop is working correctly, but the client has requested the option to attach a unique identifier to the first entry every time the for loop is run. This identifier would be used to highlight the primary ticket owner when the tickets are printed. I have included the current for loop below for reference. Any ideas on how to achieve this? Thank you.
$threepack = '';
$i = '';
for($i = 0; $i < $tickets; $i++)
{
$firstname = 'firstname'.$threepack;
$lastname = 'lastname'.$threepack;
$address = 'address'.$threepack;
$city = 'city'.$threepack;
$postal = 'postal'.$threepack;
$phone = 'phone'.$threepack;
$altphone = 'altphone'.$threepack;
$sec_firstname = 'sec_firstname'.$threepack;
$sec_lastname = 'sec_lastname'.$threepack;
$email = 'email'.$threepack;
$table->firstname = $data->$firstname;
$table->lastname = $data->$lastname;
$table->address = $data->$address;
$table->city = $data->$city;
$table->postal = $data->$postal;
$table->phone = $data->$phone;
$table->altphone = $data->$altphone;
$table->sec_firstname = $data->$sec_firstname;
$table->sec_lastname = $data->$sec_lastname;
$table->email = $data->$email;
$table->id = 0;
$table->order_total = $data->total;
$table->store();
if($data->tickets == '-1')
{
if($threepack == 2)
{
$threepack = 3;
} else {
$threepack = 2;
}
}
// 8 Fields
if($data->tickets == '5')
{
if ($threepack == '') {
$threepack = 2;
} else {
$threepack += 1;
}
}
}
for ($i = 0; $i < $tickets; $i++) {
// ...
if ($i == 0)
$table->highlightme = 1;
// ...
$table->store();
// ...
}

PHP rand function giving empty values - still not working

I have this little tid-bit of my code, which is eventually sent to a MySQL database. The rest of the code is sound, but this code likes to give me empty data some of the time. Is there any way to prevent this from happening?
Edit: Here's the whole chunk
//random Species
$sp_one = mt_rand(1,10);
$one_species = "Water Leaper";
//random Genetics
if($one_species == "Water Leaper")
{
$one_gene = mt_rand(1,5);
if($one_gene < 3)
{
$one_genetics = "1";
}
else if($one_gene < 5)
{
$one_genetics = "2";
}
else
{
$one_genetics = "3";
}
}
//random Gender
$one_sex_num = mt_rand(1,2);
if($one_sex_num == 1)
{
$one_gender = "Female";
}
if($one_sex_num == 2)
{
$one_gender = "Male";
}
//Entering it
$sql="INSERT INTO creatures (species, sex, location, genetics)
VALUES('{$one_species}','{$one_gender}', 's1','{$one_genetics}')";
mysqli_query($con,$sql);
First, your if clauses seem a bit redundant. More concise code:
if ($one_gene <3) { $one_genetics = "1"; }
elseif ($one_gene <5) { $one_genetics = "2"; }
else { $one_genetics = "3"; }
This should always return a value - if everything else fails, "3".
Maybe better even:
$one_genetics = ($one_gene + 1) / 2; // integer division
I don't know what you are doing exacly but maby you can take a look at this:
<?php
$animals = array();
$animals[] = array('dog', 78, array('Komondor','Old English Sheepdog'));
$animals[] = array('Drosophila', 8, array('Vestigal','Ebony'));
$number_animals = count($animals) - 1;
$list_size = 30;
for($q = 1; $q <= $list_size; $q++){
$rand = rand(0, $number_animals);
$animal = $animals[$rand];
$number_species = count($animal[2]) - 1;
$rand = rand(0, $number_species);
$randsex = rand(0, 1);
$species = $animal[2][$rand];
$sex = ($randsex ? 'male' : 'female');
$genes = $animal[1];
echo "$q: $species - $sex - $genes <br>";
}
?>
See a live demo at: here
It pics random animals with specs if you want you can modify to your own wishes.

Returning a default grade from a score value

Here is my problem. I upload a csv file containing two columns (student Number and Score). But when i form an HTML table from the data, i'll like to have a column display a grade according to the score uploaded... Also all the courses that have a score >40 should all be put into another table. Below is the loop i'm trying to make work, but it's not getting me anywhere near it.
Thanks for the help. I most appreciate it. Thanks
while ($row4 = mysql_fetch_assoc($query4)) {
if ($row4['score'] >= 70) {
$grade = A;
}
elseif ($row4['score'] >= 60) {
$grade = B;
}
elseif ($row4['score'] >= 50) {
$grade = C;
}
elseif ($row4['score'] >= 45) {
$grade = D;
}
elseif($row4['score'] >= 40) {
$grade = E;
}
elseif($row4['score'] >= 40) {
$grade = F;
}else {
$grade = AR;
}
}
If I understand correctly, the following should do what you are looking for. I assume that your Course ID and Student ID are in the database row, but you will need to update those if not.
<?php
define('GRADE_A_MIN', 70);
define('GRADE_B_MIN', 60);
define('GRADE_C_MIN', 50);
define('GRADE_D_MIN', 45);
define('GRADE_E_MIN', 40);
define('GRADE_F_MIN', 35);
$table_one_values = array(); // Used to create table for students with scores 40+
$table_two_values = array();
while ($row4 = mysql_fetch_assoc($query4)) {
$score = $row4['score'];
$destination_table = 'two';
if ( $score >= GRADE_A_MIN ) {
$grade = 'A';
$destination_table = 'one';
} elseif ( $score >= GRADE_B_MIN ) {
$grade = 'B';
$destination_table = 'one';
} elseif ( $score >= GRADE_C_MIN ) {
$grade = 'C';
$destination_table = 'one';
} elseif ( $score >= GRADE_D_MIN ) {
$grade = 'D';
$destination_table = 'one';
} elseif ( $score >= GRADE_E_MIN ) {
$grade = 'E';
$destination_table = 'one';
} elseif ( $score >= GRADE_F_MIN ) {
$grade = 'F';
} else {
$grade = 'AR';
}
$table = 'table_'.$destination_table.'_values';
$$table[] = array(
'course_id' => $row4['course_id'],
'student_id' => $row4['student_id'],
'score' => $score,
'grade' => $grade
);
}
Some problems:
Your $grade variable gets overwritten in the loop so you only will have the last value at the end. As your row contains a student number as well, you could do something like: $grade[$row4['number']] = 'A'; to link that specific grade to a specific student number. Note that you would set your array before the loop: $grade = array();
Your assignment is wrong, unless A, B, etc. are constants. It should probably be 'A' instead of A, etc. So: $grade[$row4['number']] = 'C';, etc.

For loop problem takes the third data as undefined

Controller
for ($x = 1; $x <= $numb; $x++)
{
echo $quanoutput = $this->input->post('quanoutput');
$barcodeoutput = $this->input->post('barcodeoutput');
$productsoutput = $this->input->post('productsoutput');
$outward_date=$this->input->post('outward_date');
$stock=$this->input->post('stock');
$warehouse_id =$this->input->post('warehouse_id');
$request_id =$this->input->post('request_id');
$warehouse=$this->input->post('warehouse');
$buyprice = $this->input->post('buyprice');
if ($productsoutput=='undefined'){
//$flag3 = $this->cartmodel->cartInsert($barcodeoutput,$quanoutput,$buyprice,$stock,$warehouse,$warehouse_id,$request_id,$outward_date);
} else {
$flag3 = $this->cartmodel->cartInsert($barcodeoutput,$quanoutput,$buyprice,$stock,$warehouse,$warehouse_id,$request_id,$outward_date);
}
}
Try starting your for loops at 0. (ie. j=0) and change the <= to just <.

Categories