A way to neaten this PHP code - php

I have the following PHP code:
<?php
//code above
$pic_1 = $afil['image_1'];
$pic_2 = $afil['image_2'];
$pic_3 = $afil['image_3'];
$pic_4 = $afil['image_4'];
$pic_5 = $afil['image_5'];
$pic_6 = $afil['image_6'];
$pic_7= $afil['image_7'];
$pic_8 = $afil['image_8'];
$pic_9 = $afil['image_9'];
$pic_10 = $afil['image_10'];
if ($pic_1 = "")
{
$pic_1 = //defaultpic - to be defined, same as below
}
if ($pic_2 = "")
{
$pic_2 = //defaultpic
}
?>
Rather than repeat these "if" statements for each picture (up until $pic 10) i just wondered if somebody could point out a more elegant and efficient way of doing it. I am quite new to php and this is a new situation i have encountered. Thanks a lot in advance.

Use arrays and loop through them with just 1 if statement, like this,
foreach($afil as $k => $v) {
if(empty($v))
$afil[$k] = ...//default pic
}
Or, if you are keen to have an additional array $pics (for future use maybe),
foreach($afil as $k => $v) {
$pics[$k] = $v;
if(empty($v))
$pics[$k] = ...//default pic
}
Also, = is an assignment operator. For comparison (or condition check), you need to use == or === (type safe).
Edit:
$afil = mysql_query(...);
while($row = mysql_fetch_array($afil)) {
//In here, say you create an array $result_rows with all the rows
$result_rows[] = $row;
}
Then, use $result_rows in the foreach.

Arrays is your golden solution:
$pic = array('image_1', 'image_2', 'image_3'....);
for ($i = 0; $i < count($pic); $i++){
if ($pic[$i] == ""){
$pic[$i] = //defaultpic -
}
}

Related

looping through txt file to use specific part of a string

I am new to Php and can't seem to figure this out no matter how much I've googled.
So I've opened the txt file (which consists of multiple lines of this type of string unique Identifier IMEI in bold:
Rx:00:39:54 06/09/2015:+RESP:GTEPS,210101,863286020022449,,8296,01,1,3,0.0,0,1031.1,29.367950,-30.799161,20150906003710,,,,,,2857.9,20150906003710,8038$) There are different strings with different IMEIs but i only want to use a specific one.
My question is, how do I extract/only use the string with the same Unique identifier and then loop through those to use in another function?
My function has different cases and each case has different calculations, so I'll need to loop through the txt file (with e.g. 863286020022449 as Identifier, ignoring other identifiers/IMEIs) in order to use the string in my function as below:
This is my starter function:
function GetParam($unknownFunction, $numberCommas) {
$returnString = "";
$foundSting = 0;
$numberFound = 0;
$len = strlen($unknownFunction);
for ($i = 0; $i < $len; ++$i) {
if ($Rawline[$i] == ",") {
++$numberFound;
if ($numberFound > $numberCommas)
break;
if ($numberFound == $numberCommas)
$foundSting = 1;
}
else if ($foundSting == 1) {
$returnString .= $unknownFunction[$i];
}
}
return $returnString;
echo $returnString;
}
$i = strpos($unknownFunction, ":GT");
$p = substr($unknownFunction, $i+3,3);
$Protocol = GetParam($unknownFunction, 1);
//this switch reads the differences in the message types (e.g. HBD- in this case is a heartbeat message type and would thus have a different amount of commas in the string and has different definitions of the characters within the commas)
switch ($p) {
case 'HBD':
//+ACK:GTHBD,220100,135790246811220,,20100214093254,11F0$
//This is an example of an HBD message
$result2["Type"] = 'Heart beat';
$IMEI = GetParam($unknownFunction, 2);
$mDate = GetParam($unknownFunction, 4);
$mDate = substr($mDate,0,4).'-'.substr($mDate,4,2).'-
'.substr($mDate,6,2).'
'.substr($mDate,8,2).':'.substr($mDate,10,2).':'.substr($mDate,12,2);
break;
This is the biggest problem I am facing at the moment and when I print the different lines, it indicates the correct IMEI but it does not loop through the whole file to use each string that belongs to that IMEI.
Your assistance would be greatly appreciated.
Thank you so much.
Example of input file:
Rx:00:00:00 28/02/2018:+RESP:GTFRI,3C0103,862045030241360,,14067,11,1,1,29.7,320,151.1,30.949307,-29.819685,20180227235959,0655,0001,013A,87B6,00,35484.1,01500:51:31,,,100,220101,,,,20180228000000,3461$
Rx:00:00:01 28/02/2018:+RESP:GTERI,380201,869606020047340,gv65,00000002,14076,10,1,1,119.0,119,24.3,18.668516,-34.016808,20180227235955,0655,0001,00F7,2DC9,00,98912.0,02235:20:25,0,100,220101,0,0,20180227235958,FF20$
Rx:00:00:03 28/02/2018:+RESP:GTERI,380201,869606020162990,,00000002,12912,10,1,1,0.0,230,1127.3,30.846671,-27.674206,20180227235956,0655,0001,013E,88B0,00,106651.1,03546:44:42,0,100,210101,0,0,20180227235959,6190$
Rx:00:00:03 28/02/2018:+ACK:GTHBD,450102,865084030005340,gb100,20180228000003,CC61$
Rx:00:00:03 28/02/2018:+RESP:GTERI,380201,869606020115980,,00000002,13640,10,1,1,12.1,353,1663.1,28.580726,-28.162208,20180227235957,,,,,,37599.6,02422:07:24,0,100,220101,0,0,20180228000000,1937$
Rx:00:00:04 28/02/2018:+RESP:GTERI,380502,869606020276840,gv65,00000002,12723,10,1,1,0.0,106,1232.8,22.878013,-27.951762,20180227235952,0655,0001,0204,63C5,00,13808.9,00778:32:20,0,100,210100,0,0,20180228000002,2C50$
Rx:00:00:04 28/02/2018:+RESP:GTERI,380502,869606020274530,gv65,00000002,12683,10,1,1,0.0,91,1213.7,24.863444,-28.174319,20180227235956,0655,0001,0203,69F1,00,9753.2,00673:49:21,0,100,210100,0,0,20180228000003,8AC7$
Rx:00:00:05 28/02/2018:+ACK:GTHBD,380201,863286023083810,,20180228000003,0D87$
Rx:00:00:06 28/02/2018:+RESP:GTFRI,3C0103,862045030241360,,14086,10,1,1,34.0,327,152.0,30.949152,-29.819501,20180228000002,0655,0001,013A,87B6,00,35484.1,01500:51:36,,,100,220101,,,,20180228000005,3462$
Rx:00:00:06 28/02/2018:+ACK:GTHBD,060228,862894021626380,,20180228000007,F9A5$
Rx:00:00:07 28/02/2018:+RESP:GTERI,380201,869606020019430,,00000002,12653,10,1,1,0.0,219,1338.7,26.882063,-28.138099,20180228000002,,,,,,86473.7,05645:48:34,0,93,210101,0,0,20180228000003,0FA5$
Rx:00:00:09 28/02/2018:+ACK:GTHBD,380502,869606020233940,gv65,20180228000008,7416$
Rx:00:00:10 28/02/2018:+RESP:GTAIS,380201,869606020171710,,11,11,1,1,0.0,95,281.2,30.855164,-29.896575,20180228000009,0655,0001,0156,9A9F,00,156073.7,20180228000008,F9A4$
Each GT message means something which is why i need to extract only one specific IMEI and use the result in my function as a breakdown of what every set of numbers between the commas actually mean. The end result needs to be populated in an excel spreadsheet but that's a different issue.
Nested foreach, keeping tracking of the IMEIs you've already gone through. Or something like this.
<?php
$filename = 'info.txt';
$contents = file($filename);
foreach ($contents as $line) {
$doneAlreadyArray = array();
$IMEI = GetParam($line, 2);
foreach ($contents as $IMEIline){
$thisIMEI = GetParam($IMEIline,2);
//check if already done the IMEI previously
if (!in_array($thisIMEI, $doneAlreadyArray)){
//matching IMEIs?
if ($thisIMEI == $IMEI){
//run new function with entire $IMEIline
new_function($IMEIline);
}
}
}
//add IMEI to doneAlreadyArray
array_push($doneAlreadyArray,$IMEI);
}
?>
If I've understood your question right and you want to extract the string(line) with the same Unique identifier, this may be useful for your needs as a strating point.
The example is very basic, and use data from your question:
<?php
// Read the file.
$filename = 'input.txt';
$file = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
// Each item of $output will contain an array of lines:
$output = array();
foreach ($file as $row) {
$a = explode(',', $row);
$imei = $a[2];
if (!array_key_exists($imei, $output)) {
$output[$imei] = array();
}
$output[$imei][] = $row;
}
// Then do what you want ...
foreach ($output as $key=>$value) {
echo 'IMEI: '.$key.'</br>';
foreach($value as $row) {
// Here you can call your functions. I just echo the row:
echo $row.'</br>';
}
}
?>
thank you for the feedback.
Ryan Dewberry ended up helping me.
The fix was simpler than I thought too :)
//Unknownfunction is now $line
function GetParam($line, $numberCommas) {
$returnString = "";
$foundSting = 0;
$numberFound = 0;
$len = strlen($line);
for ($i = 0; $i < $len; ++$i) {
if ($line[$i] == ",") {
++$numberFound;
if ($numberFound > $numberCommas)
break;
if ($numberFound == $numberCommas)
$foundSting = 1;
}
else if ($foundSting == 1) {
$returnString .= $line[$i];
}
}
return $returnString;
// print $returnString;
}
//this is new - makes sure I use the correct IMEI
$contents = file($fileName);
foreach ($contents as $line){
$haveData = 0;
$IMEI = GetParam($line, 2);
if ($IMEI == $gprsid){
$i = strpos($line, ":GT");
$p = substr($line, $i+3,3);
$Protocol = GetParam($line, 1);
//this is the part I struggled with as well - This is an array of all of my
//calculation
//results and in printing it out I can see that everything is working
$superResult = array();
array_push($superResult,$result2);
print_r($superResult);
}
}
Much appreciated. Thank you!

Best way to check if lots of variables contain a string and if so change it?

I have lots of variables that I need to check to see if it is equal to "None". If a variable is equal to "None" I would like to change it to = "-";
What's the best practice way of doing this without having a separate IF function for each variable?
//Variables
$profileEthnicity = h($result["profile_ethnicity"]);
$profileHeight = h($result["profile_height"]);
$profileBuild = h($result["profile_build"]);
$profileEyeColor = h($result["profile_eye_color"]);
$profileHairColor = h($result["profile_hair_color"]);
$profileTattoos = h($result["profile_tattoos"]);
$profilePiercings = h($result["profile_piercings"]);
//Example
if($profileEthnicity == "None") { $profileEthnicity = "-"; }
Since the values are in an array you can do this:
foreach($result AS $key => $val) {
('None' == $val) ? $result[$key] = '-' : $result[$key] = $val;
}

PHP store a value from a While loop in an associative manner for external use?

This is only a personal project. So yes, it's using mysql, but it's not commercial and only running on localhost. So, not really imperative I update it to mysqli or anything. I'm also not a developer and dabble with php for personal databases. So, forgive me if my code is horrendous, I'm eager to improve if you'd care to share.
I've constructed a while loop to fill arrays. This allows me to check against those arrays in another while loop. The goal being to improve efficiency and not just run loops, within loops, within loops.
For the most part this works fine. I'm trying to extend the capabilities though and hitting a hurdle.
// query to pull data. / GROUP BY id
// pulls multiple results, but I merely want the results for each distinct ID.
$r = 0;
$u = 0;
$n = 0;
$checkarr=array();
$mustarr=array();
$badarr=array();
$idarr=array();
while ($row = mysql_fetch_array($qry)) {
$check = $row['tbl.check'];
$must = $row['tbl.must'];
$thisid = $row['tbl.id'];
if ($check == 1) {
$checkarr[] = $thisid;
$n++; }
if ($must == 'R') {
$badarr[] = $thisid;
$r++; }
if ($must == 'U') {
$mustarr[] = $thisid;
$u++; }
}
This works to fill the arrays properly with the ID for comparison later. I can just run in_array([id], $checkarr) to see if they match.
What I'm trying to do and failing miserably at... is to fill some sort of associative array so that each $thisid is associated with the final values of $n, $r, and $u. That way, later I can somehow call $thisid['r'] and get the total value of $r associated with that ID.
Something like....
(Pseudo code)
if((in_array($loopid, $mustarr)) {
//display value of $r associated with the $loopid
}
I've tried foreach() loops and just am not figuring it out properly.
How can I associate the total values of $n, $r, and $u with $thisid? Primarily so that I can pull each value outside of this specific loop?
Any tips would be appreciated.
You could make $thisid and your other arrays associative arrays.
$r = 0;
$u = 0;
$n = 0;
$checkarr = array();
$mustarr = array();
$badarr = array();
$idarr = array();
while ($row = mysql_fetch_array($qry)) {
$check = $row['tbl.check'];
$must = $row['tbl.must'];
$thisid = array('id' => $row['tbl.id'], 'n' => $n, 'r' => $r, 'u' => $u);
if ($check == 1) {
$checkarr[$row['tbl.id']] = $thisid; // To make it easier to find $thisid later
$n++; }
if ($must == 'R') {
$badarr[$row['tbl.id']] = $thisid;
$r++; }
if ($must == 'U') {
$mustarr[$row['tbl.id']] = $thisid;
$u++; }
}
If you want to check, if an id is in one of the arrays you could use array_key_exists:
if(array_key_exists($loopid, $mustarr)){
echo $mustarr[$loopid]['r']; // Displaying $r for $loopid
}
if I understand right you can just
if ($must == 'R') {
$badarr[$r] = $thisid;
$r++; }
and you can get value of id by number of iteration
or Vice versa
if ($must == 'R') {
$badarr[$thisid] = $r;
$r++; }
get number of iteration by id

Why and when to use &$result in PHP?

We are trying to call the same function from within the function but it won't work.
We belive that there is a &$result variable that should be placed somewhere , we have seen it in other parts of our code that another person has written, but we don't know where and how it works.
Could someone please explain?
Here is our code if you want to have a look:
$parentID = $_POST['id'];
$choosenCategory = $_POST['choosenCategory'];
$count = 0;
function count_child($parentID, $choosenCategory){
foreach($parentID as $thisID){
foreach($_SESSION['items'][$thisID]['Children'] as $ChildID){
$DatabaseID = $ChildID;
$ItemCategory = $_SESSION['items'][$DatabaseID]['ItemCategory'];
$ItemName = $_SESSION['items'][$DatabaseID]['ItemName'];
$ItemStatus = $_SESSION['items'][$DatabaseID]['ItemStatus'];
$ParentID = $_SESSION['items'][$thisID]['DatabaseID'];
$Children = $_SESSION['items'][$DatabaseID]['Children'];
$Dependencies = $_SESSION['items'][$DatabaseID]['Dependencies'];
if($ItemCategory == $choosenCategory){
$count++;
}
if($ItemCategory !== "RWP" && $ItemCategory !== "US" && $levels === "all"){
$array = array();
count_child($ChildID, $choosenCategory);
}
}
}
}
count_child($parentID, $choosenCategory);
$json = json_encode($count);
echo $json;
It always output 0, regardless of what input we give.
Try to return $count from the function;
Like this:
$parentID = $_POST['id'];
$choosenCategory = $_POST['choosenCategory'];
function count_child($parentID, $choosenCategory){
$count = 0;
foreach ($parentID as $thisID){
$aChild = &$_SESSION['items'][$thisID]['Children'];
foreach ($aChild as $ChildID){
$DatabaseID = $ChildID;
$ItemCategory = $_SESSION['items'][$DatabaseID]['ItemCategory'];
$ItemName = $_SESSION['items'][$DatabaseID]['ItemName'];
$ItemStatus = $_SESSION['items'][$DatabaseID]['ItemStatus'];
$ParentID = $_SESSION['items'][$thisID]['DatabaseID'];
$Children = $_SESSION['items'][$DatabaseID]['Children'];
$Dependencies = $_SESSION['items'][$DatabaseID]['Dependencies'];
if ($ItemCategory == $choosenCategory){
$count++;
}
if ($ItemCategory !== "RWP" && $ItemCategory !== "US" && $levels === "all"){
$array = array();
paint_child($ChildID, $choosenCategory);
}
}
}
return $count;
}
$count = count_child($parentID, $choosenCategory);
$json = json_encode($count);
echo $json;
Depending on the php version and which datatype you are using, php creates a copy of your data when you call the function. that copy is used within that function. when you are now modify the data then the copy is modify. Later when your call is finished, the copy will be deleted (in each function call in every recursion step). That & operation avoids a copy and sends a reference. No copy will be created. The reference operator can used in function parameters also at return values, but it's more common in function parameters.

Assign a value conditionally after array is set with PHP

If I wanted to do something like this:
<?php
$numbers = array(
"a_pos" => 0,
"b_pos" => 2,
"c_pos" => 3
);
if ($numbers["a_pos"] == 0)
$a_pos_txt = TRUE;
if ($numbers["b_pos"] == 0)
$b_pos_txt = TRUE;
if ($numbers["c_pos"] == 0)
$c_pos_txt = TRUE;
?>
(Just assign TRUE to $a_pos_txt because it is equal to 0)
What would be that smart way to do it? I´m sure there must be a way to do it in "one step".
Thanks in advance!!
Please ask for any needed clarificarion.
Not really sure what you're trying to accomplish, as there may be a better approach overall, but to answer your question, you can skip the if statements like so:
$a_pos_txt = $numbers["a_pos"] == 0;
$b_pos_txt = $numbers["b_pos"] == 0;
$c_pos_txt = $numbers["c_pos"] == 0;
If the $numbers is an array, you can do a loop to avoid repeating the similar pattern,
such as
foreach ($numbers as $key=>$val)
{
if ($val==0)
{
${$key."_txt"}=true;
}
}

Categories