MySQL ORDER BY and while loop - php

I already asked a question belonging this issue here
The code from the accepted answer by RiggsFolly works really proper but there is a small issue with that. I needed some days to test it out and searched for the reason why this is not the best way to solve the main goal. I was pleased to open a new question.
The code from RiggsFolly is based on $current_provider so the while-loop checks on every round if $current_provider has changed. So far so good. BUT now I needed to add a comprehensive logic. It meens that I added a true/false variable that simply checks if a value from an fetched object is equal to a certain string. This comparison is focused on a specific list item and not to the basic $current_provider.
So the goal is that $current_provider checks each fetched object for true/false and will be independent from $current_provider. At the moment I try to extend with a second loop but just want to give an example in the hope that it will be clear what to achieve:
$service = $db->query("SELECT * FROM `system` ORDER BY provider, artist");
$provider = NULL;
$close = false;
while ($data = $service->fetch_object()) {
$amount_1 = $data->digit_1; //db-structure: float
$amount_2 = $data->digit_2; //db-structure: float
if ($amount_1 == $amount_2) {
$close = true;
}
if ( $current_provider != $data->provider ) {
if ( $current_provider !== NULL ) {
echo '</div>close container in case of current_provider is already set';
}
echo '<div class="provider">open a new container in case of current_provider is not like data->provider or empty';
$current_provider = $data->provider;
}
echo 'some styling for each object';
if ($close === true ) {
echo '<div class="specific">if the amount_1 is same like amount_2 for a single object add only for this object a certain div';
} else {
echo '<div>show standard container even on specific object';
}
echo '</div><!--close container provider-->';
}
Kind regards.

I am still not sure I understand what you are actually trying to achieve here but maybe if you lay your decision making out like this it will all seem more obvious what you may need to do in addition to what I am suggesting.
The moving of
$amount_1 = $data->digit_1;
$amount_2 = $data->digit_2;
from a perfectly good object properties to 2 scalar variables is totally unnecessary why store everything twice, you will eventually run out of memory, but more importantly if you leave them in the object and test them using if ($data->digit_1 == $$data->digit_2) { its never confusing where this data came from!
Also testing the digits at the top of the loop only to set ANOTHER scalar variable to use later at the bottom of the loop is a waste of time. Those properties dont change between top and bottom of the loop so test the actual object where you want to make the decision and then put out the required HTML right there and then. Another potential confusion averted and 8 to 16 bytes of memory not wasted!
$service = $db->query("SELECT * FROM `system` ORDER BY provider, artist");
$current_provider = NULL;
while ($data = $service->fetch_object()) {
if ( $current_provider != $data->provider ) {
if ( $current_provider !== NULL ) {
echo '</div>close container in case of current_provider is already set';
}
echo '<div class="provider">open a new container in case of current_provider is not like data->provider or empty';
$current_provider = $data->provider;
}
echo 'some styling for each object';
// at this point we test the 2 digits and if equal
// add an extra bit of HTML to the output
if ($data->digit_1 == $data->digit_2) {
echo '<div class="specific">if the amount_1 same like amount_2 for a single object add only for this object an certain div';
} else {
echo '<div>show standard container even on specific object';
}
echo '</div>;
}

To avoid playing with opening and closing element better to store them in array and in the end output them.
Look at my example, it's simple:
$service = $db->query("SELECT * FROM `system` ORDER BY provider, artist");
$provider = NULL;
$lines = array();
while ($data = $service->fetch_object()) {
$close = false;
if ($something === $another) {
$close = true;
}
if ( $provider != $data->provider ) {
$lines[] = '<div class="provider">'.$data->provider.'</div>';
$provider = $data->provider;
}
if ($close === true ) {
$lines[] = '<div class="specific">add container for just a specific object when close === true within the while loop</div>';
} else {
$lines[] = '<div>show standard container on specific object</div>';
}
}
foreach($lines AS $line) {
echo $line;
}

Related

Comparison of words from the database and output of the result

I need to check the words received from the database with the user's entered word and if there is a match, then output its value from the database, and if not, then output what the user entered.
The code below works fine if there is a match.
function d_typeplace_morf($d_typeplace)
{
global $wpdb;
$typeplace_results = $wpdb->get_results('SELECT vozmozhnyi_variant_mesta, ego_slovoforma_v_predlozhnom_padezhe FROM dEzpra_jet_cct_tip_mest_obrabotki');
if ($typeplace_results) {
foreach ($typeplace_results as $typeplace_result) {
$d_typeplace_raw = mb_strtolower($typeplace_result->vozmozhnyi_variant_mesta);
$d_typeplace_morf = mb_strtolower($typeplace_result->ego_slovoforma_v_predlozhnom_padezhe);
$d_typeplace = mb_strtolower($d_typeplace);
if (stripos($d_typeplace, $d_typeplace_raw) !== false) {
echo $d_typeplace_morf;
}
}
}
}
I'm an amateur in PHP, just learning. And I can't figure out how to output $d_typeplace if no match is found.
I tried to add
else {
echo $d_typeplace;
}
, but I get an array of words from the user entered.
I will be grateful for any help. Also for any suggestions for improving this code.
---Addition---
I apologize for my English. This is a problem in the Russian language, I need to take into account the morphology. To do this, the database has a list of words and their analog, for example, X = Y. I get these words and compare what the user entered. If he entered X, then we output Y. If he led Z, which is not in the database, then we output Z.
Thus, we check $d_typeplace with $d_typeplace_raw and if there is a match, we output $d_typeplace_morf, which is equal to $d_typeplace_raw. And if not, then $d_typeplace (it contains the value that the user entered).
Oh, I'm sorry, I understand myself that I'm explaining stupidly)
I cannot quite understand what you are asking: you need to output the string entered by the user, but you can only print an array?
If this is the case, I think you parsed the string before, in order to therefore you need to do join again the values contained in the array.
Try with:
else {
echo implode(" ", $d_typeplace);
}
--- EDITED ---
Try with:
function d_typeplace_morf($d_typeplace)
{
global $wpdb;
$typeplace_results = $wpdb->get_results('SELECT vozmozhnyi_variant_mesta, ego_slovoforma_v_predlozhnom_padezhe FROM dEzpra_jet_cct_tip_mest_obrabotki');
if ($typeplace_results) {
$found = false;
foreach ($typeplace_results as $typeplace_result) {
$d_typeplace_raw = mb_strtolower($typeplace_result->vozmozhnyi_variant_mesta);
$d_typeplace_morf = mb_strtolower($typeplace_result->ego_slovoforma_v_predlozhnom_padezhe);
$d_typeplace = mb_strtolower($d_typeplace);
if (stripos($d_typeplace, $d_typeplace_raw) !== false) {
echo $d_typeplace_morf;
$found = true;
break;
}
}
if (!$found) {
echo $d_typeplace;
}
}
}
But I think it would be more efficient, if you implemented the second code snippet written by #Luke.T
I'm presuming you were trying to add the else like this?
function d_typeplace_morf($d_typeplace)
{
global $wpdb;
$typeplace_results = $wpdb->get_results('SELECT vozmozhnyi_variant_mesta, ego_slovoforma_v_predlozhnom_padezhe FROM dEzpra_jet_cct_tip_mest_obrabotki');
if ($typeplace_results) {
foreach ($typeplace_results as $typeplace_result) {
$d_typeplace_raw = mb_strtolower($typeplace_result->vozmozhnyi_variant_mesta);
$d_typeplace_morf = mb_strtolower($typeplace_result->ego_slovoforma_v_predlozhnom_padezhe);
$d_typeplace = mb_strtolower($d_typeplace);
if (stripos($d_typeplace, $d_typeplace_raw) !== false) {
echo $d_typeplace_morf;
} else {
echo $d_typeplace;
}
}
}
}
Which was outputting an array because the for loop was continuing, if you add a break like so...
echo $d_typeplace;
break;
It should stop outputting an array. Depending on your use case you could however perform similar functionality directly in your sql query using LIKE ...
function d_typeplace_morf($d_typeplace)
{
global $wpdb;
$typeplace_results = $wpdb->get_results('
SELECT ego_slovoforma_v_predlozhnom_padezhe
FROM dEzpra_jet_cct_tip_mest_obrabotki
WHERE vozmozhnyi_variant_mesta LIKE %' . $d_typeplace . '%');
if ($typeplace_results) {
//Echo result
} else {
echo $d_typeplace;
}
}

php loop flip coin until 2 heads, then stop

I'm new to php and am trying to write a loop that will flip a coin until exactly two heads have been flipped and then stop.
So far I've written a function for coin flipping:
function cointoss () {
$cointoss = mt_rand(0,1);
$headsimg = '<img src=""/>';
$tailsimg = '<img src=""/>';
if ($cointoss == 1){
print $headsimg;
} else {
print $tailsimg;
}
return $cointoss;
}
...but am stuck on writing the loop. I've tried a couple ways:
#this code takes forever to load
$twoheads = 0;
for ($twoheads = 1 ; $twoheads <= 20; $twoheads++) {
$cointoss = mt_rand(0,1);
cointoss ();
if ($cointoss == 1) {
do {
cointoss ();
} while ($cointoss == 1);
}
}
#one coin flips
do {
cointoss ();
} while ($cointoss == 1);
This is a for a class, and we haven't learned arrays yet, so I need to accomplish this without them.
I understand the concept of loops executing code while a condition is true, but don't understand how to write for when a condition is no longer true.
Printing from inside of "processing functions" is a bad habit to get into. You might like to declare a showCoin($toss) function for printing. In truth, I don't know if I would bother with any custom functions.
You need to declare a variable which will hold the return value from your function.
By storing the current and previous toss values, you can write a simple check if two consecutive "heads" have occurred.
Code: (Demo)
function cointoss () {
return mt_rand(0,1); // return zero or one
}
$previous_toss = null;
$toss = null;
do {
if ($toss !== null) { // only store a new "previous_toss" if not the first iteration
$previous_toss = $toss; // store last ieration's value
}
$toss = cointoss(); // get current iteration's value
echo ($toss ? '<img src="heads.jpg"/>' : '<img src="tails.jpg"/>') , "\n";
// ^^^^^- if a non-zero/non-falsey value, it is heads, else tails
} while ($previous_toss + $toss != 2);
// ^^^^^^^^^^^^^^^^^^^^^^- if 1 + 1 then 2 breaks the loop
Possible Output:
<img src="heads.jpg"/>
<img src="tails.jpg"/>
<img src="tails.jpg"/>
<img src="tails.jpg"/>
<img src="heads.jpg"/>
<img src="heads.jpg"/>

How to set a variable value based on conditions set by other variables

So I am new to PHP, and am currently just doing a little project as practice, I've managed to get down a few lines of code without falling over... but am a bit stuck here.
Essentially, what my script currently does is check three different variables that I have set (each a true/false option) and distinguishes if there is only one true option selected (out of the 3 options, only one can be true, the other 2 must be false). If only 1 value is set to true, the rest of the code runs; if multiple values are set to true, or no values are set to true, it shows an error prompt for the user.
Once this check is done, I wanted to then set the value of $name for example, based on records linked to the relevant variable that is true... This is what I have come up with, but it doesn't seem to work...
if ($value1 == "true") {$name = $result1;}
else if ($value2 == "true") {$name = $result2;}
else if ($value3 == "true") {$name = $result3;}
else exit (0)
So i essentially want to set the $name variable by identifying which of the 3 value variables is true, and then setting $name with the relevant variable retrieved in the $result
Any help would be appreciated. And before anyone goes off on one... I know I may sound a bit mad... but we all have to start somewhere!!
Thanks
It would look much nicer with a switch:
switch(true){
case $value1:
$name = $result1;
break;
case $value2:
$name = $result2;
break;
case $value3:
$name = $result3;
break;
default:
exit();
}
In case you need to make sure only one of the statements is true, validate that prior using this:
//In case you need to make there is only a single true statement
$found = false;
for($i=1; $i<4; $i++) {
$value = "value".$i;
if($$value) {
if($found) {
exit("More than one 'true' statement");
}
$found = true;
}
}
Dracony's answer looks nice indeed, but will fail when multiple values are set to true. For more flexibility, you should consider mapping the values into arrays, and tracking the state (amount of values that are true) with a flag variable. Find a fully commented example that will satisfy all conditions below. Additionally, this code will work with arrays of any length (you can add conditions by simply putting more values in $values and $results).
// store the data in arrays for easier manipulation
$values = array($value1, $value2, $value3);
$results = array($result1, $result2, $result3);
// set a flag to check the state of the condition inside the loop
// it keeps track of the index for which the value is true
$flag = -1;
$name = NULL;
// use for and not foreach so we can easily track index
for ($i = 0; $i < count($values); $i++) {
// if no index has been found 'true' yet, assign the current result.
if ($values[$i] === true) {
if ($flag === -1) {
$flag = $i;
$name = $results[$i];
}
// if a 'true' value has been found for another index
// reset the name var & stop the loop
if ($flag > -1 && $flag !== $i) {
$name = NULL;
break;
}
}
}
if ($name) {
// run code when only 1 value is true
} else {
exit();
}

If Else Echo JSON array check

I have a JSON array that I am pulling values from per $vars. Within the JSON data are going to be some key words that I am looking for. I have a single if else that looks like:
(demonstration purposes)
if( $FullName == $Data[$c]['manager'] $FullName == $Data[$c]['leader'] || $FullName == $Data[$c]['helper']) {
$cheapLabor = 'NO';
} else {
$cheapLabor = 'YES';
}
That works great however, now I want to define more specifically some if else points on status points which would represent their employement status. Each Emp Status is based on a group.
I would need it to check from the top of the food chain, then go downward to check if status = x. If it does then $cheapLabor = 'y'; else $cheapLabor = 'z';
I tried doing it, but I can't seem to get it to work. Here is what I am working with:
$repData = json_decode($json, TRUE);
$c = 0;
$var = $repData[$c]['column'];
if($FullName == $repData[$c]['ceo']) {
$groups = '[13]';
} else {
$groups = '[5]';
}
if($FullName == $repData[$c]['director']) {
$groups = '[10]';
} else {
$groups = '[5]';
}
if($FullName == $repData[$c]['regional']) {
$groups = '[9]';
} else {
$groups = '[5]';
}
if($FullName == $repData[$c]['project_manager']) {
$groups = '[8]';
} else {
$groups = '[]';
}
if($FullName == $repData[$c]['team_leader']) {
$groups = '[6]';
} else {
$groups = '[5]';
}
if($FullName == $repData[$c]['rae']) {
$groups = '[5]';
} else {
$staus = '[5]';
}
Shomz Answer partial working...
$groups = '[4]'; // new hire group default, to be overwritten if a user has the correct title within Table.
$roleGroups = array(
'regional' => '[7]',
'team_leader' => '[6]',
'RAE' => '[5]'
);
foreach ($roleGroups as $role => $groups) { // go through all the Position Titles
if ($FullName == $repData[$c][$role]) { // see if there's a match
$repGroup = $groups; // if so, assign the group
}
}
It sets team_leader and regional correctly but anything else just sets it as regional group.
Just realized that its actually rewriting the value.
Your code is overwriting $groups in every if-statement. You probably want to rewrite that in a switch/case statement with a default value being [5].
Let's say the first if is true, so $FullName == $repData[$c]['ceo'] is true and $groups becomes [13]. In the next line, there are two choices:
either a person is a director (AND a CEO, but it doesn't matter, see why below)
or a person is not a director (could be a CEO)
In both cases, $groups will either get a value of [10] or [5], meaning that no matter what happened inside the statement above, this statement will overwrite it. Thus, only your last if statement is able to produce results you might expect.
"Only one group per role"
In that case a simple switch/case statement will work:
switch($FullName){
case ($repData[$c]['ceo']):
$groups = '[13]';
break;
case ($repData[$c]['director']):
$groups = '[10]';
break;
// etc... for other roles
default:
$groups = '[5]';
break;
}
Or you can go even simpler and use an associative array to combine roles with group numbers. For example:
$roleGroups = array('ceo' => '[13]', 'director' => '[15]', etc);
Then simply see if there's a match:
$groups = '[5]'; // default, to be overwritten if a role is found below
foreach ($roleGroups as $role => $group) { // go through all the groups
if ($FullName == $repData[$c][$role]) { // see if there's a match
$groups = $group; // if so, assign the group
}
}
Hope this makes sense. Either way, $groups will have the number of the role if role is found, 5 otherwise.

Define value edit by another php file

I have 2 questions
1.) how to write update_defile($array_value){...} function?
define_file.php
<?php
define("FIST_NAME", "something1");
define("LAST_NAME", "something2");
define("ADDRESS", "something3");
?>
"something" is not a constant value that can be change every method Call(update_defile($array_value)
value set
$array_value = ("FIST_NAMe" => "duleep", "LAST_NAME" => "dissnayaka", "AGE" => "28" );
after call method(update_defile($array_value){.....}) "define_file.php"
file want to be look like bellow
<?php
define("FIST_NAME", "duleep");
define("LAST_NAME", "dissnayaka");
define("ADDRESS", "something3");
define("AGE", "28");
?>
2).
My datbase is Oracle. I already saved configuration value in the data base but frequently use these configuration value for my application. So i get value form database and save in the define_file.php as increase performance(down rate database call) but I'm not sure i can increase performance keep configuration value in the PHP file please explain. what is the best way increase performance my application and other alternative solutions welcome.
Why cant u use session to store such values , then u can access and modify from anywhere
in the script.
<?php
session_start();
$_SESSION["FIST_NAME"]= "something1";
$_SESSION["LAST_NAME"]= "something2";
$_SESSION["ADDRESS"]= "something3";
?>
public function update($form_config_arr)
{
if( (is_readable($config_file_path)) && is_writable($config_file_path))
{
if(!$config_old_file_content = file_get_contents($config_file_path))
{
throw new Exception('Unable to open file!');
}
$i = 0;
$config_old_arr = array();
$config_new_arr = array();
foreach ($form_config_arr as $constant => $value){
$config_old_line = $this->getLine($constant);
$config_old_arr[$i] = $config_old_line;
if(($value == 'true') || ($value == 'false')){
$config_new_arr[$i] = "define ( '$constant', $value );\n";
}else{
$config_new_arr[$i] = "define ( '$constant', '$value' );\n";
}
$i++;
}
$config_new_file_content = str_replace($config_old_arr, $config_new_arr, $config_old_file_content);
$new_content_file_write = file_put_contents($config_file_path, $config_new_file_content);
foreach ($config_new_arr as $constant => $value)
{
echo $value.'<br/>';
}
return true;
}else{
throw new Exception('Access denied for '.$config_file_path);
return false;
}
}
/**
*
* #param string $constant
* #return string
*/
private function getLine($constant)
{
$match_line = '';
$config_file = fopen($config_file_path, "r");
if($config_file)
{
//Output a line of the file until the end is reached
$i = 0;
while(!feof($config_file))
{
$i++;
$config_old_line = fgets($config_file);
$pos = strpos($config_old_line, $constant);
if( $pos !== false )
{
$match_line= $config_old_line;
}
}
fclose($config_file);
return $match_line;
}else{
throw new Exception('Unable to open file!');
}
}
What you are trying to do is edit a file.
Simply create another php script: updater.php
It should poll the database, fetch the values and update the values in define_file.php
Look for php file handling functions here: http://www.w3schools.com/php/php_file.asp

Categories