I want to get employee name from $projSDE_1 that consists of emp ID for each row in the for loop below from function func_GetEmpName($empID).For the code below it should display 10 rows with $projSDE_1's namebut the problem is the code outputs only the 1st row .Why the other 9 rows could not be display?Im already stuck with this problem for 1 week.If i remove the function it will display 10 rows
.All help appreciated.
<?php
for($i=0; $i<db_rowcount();$i++){
//loop through every result set
$projID=db_get($i,0);
$projNo=db_get($i,1);
$projDesc=db_get($i,2);
$projSDE_1=db_get($i,8);//1st Services Development Engineer
$projSDE_1_name = func_GetEmpName($projSDE_1);//get name from function
}//endfor
function func_GetEmpName($empID) {
$sqlEmp="select EmpID,LastName2_c from empbasic WHERE EmpID= '".$empID."'";
db_select($sqlEmp);
$rowcount=db_rowcount();
if(db_rowcount()>0){
for($f=0;$f<count($empID);$f++){
$empID=db_get($f,0);
$empName=db_get($f,1);
}
}
return $empName;
} // function
?>
this the output from the code
need to display like this
removed function
As CBroe said, you're overwritting $empName in each loop. Put the variable in an array to avoid that.
<?php
$projSDE_1_IDs = [];
for($i=0; $i<db_rowcount();$i++){
//loop through every result set
$projID=db_get($i,0);
$projNo=db_get($i,1);
$projDesc=db_get($i,2);
$projSDE_1=db_get($i,8);//1st Services Development Engineer
array_push($projSDE_1_IDs, $projSDE_1) // Store all `$projSDE_1` in an array
}//endfor
$projSDE_1_names = func_GetEmpName($projSDE_1_IDs); // Pass the IDs' array to the function
function func_GetEmpName($empIDs) {
$names = [];
foreach($empIDs as $empId){
$sqlEmp="select EmpID,LastName2_c from empbasic WHERE EmpID= '".$empID."'";
db_select($sqlEmp);
$rowcount=db_rowcount();
if(db_rowcount()>0){
for($f=0;$f<count($empID);$f++){
$empID=db_get($f,0);
$empName=db_get($f,1);
array_push($names, $empName)
}
}
}
return $names;
} // function
var_dump($projSDE_1_names) // Display the array to see if you get all the correct data
Not very sure for the foreach part in the function but yb modifying little thing by your own if it's not working, you should be able to do what you want. I think the logic is here.
Related
I'm working with data from Formstack's REST API. Everything is in XML format. I wrote some PHP to process it, and for some reason when I echo $valueVar in a foreach loop only the value from the first entry is returning for each of the three entries. I really don't know why the string isn't getting replaced. Could someone please shed some light on what I'm doing wrong.
Thank you!
PHP Output
Formstack API XML Output
foreach($xml->submissions->submission as $submission) {
foreach($xml->submissions->submission->data->XML_Serializer_Tag as $serializer) {
$fieldeVar = (string)$serializer->field;
$valueVar = (string)$serializer->value;
if ($fieldeVar=="95733876") {
echo $valueVar;
break;
}
}
}
I think this is a simple case of your 2 loops clobbering each other. Notice how you're defining the inner loop as foreach($xml->submissions->submission->.... That will always grab the first instance. It should be using $submission which your outer loop is setting up for you.
Just change
foreach($xml->submissions->submission as $submission) {
foreach($xml->submissions->submission->data->XML_Serializer_Tag as $serializer) {
to this
foreach ($xml->submissions->submission as $submission) {
foreach ($submission->data->XML_Serializer_Tag as $serializer) {
Output
prefix = Dr first = Argnes last = Acmefirst = Bobby last =
Leamalotfirst = Tina last = Students
I have an array that looks like this:
Id = "ADA001"
Stock: 15
The array has about 1700 records that looks the same, how would I search the array for the ID 1 and return the stock?
Edit: I will need to access the stock of each one of these 17000 records
Edit: I have been getting some help from Daniel Centore, he told me to set an arrays primary key to the id of the item and that it is equal to the stock, but I can't get it to work.
I am currently getting the data from an MySQL database and I store it in an array, like so:
$data[] = array();
$getdisposabletest = mysqli_query($connect, "Select id, disposable FROM products");
while ($z = mysqli_fetch_array($getdisposabletest)) {
$data = $z;
}
Now when I use Daniels code that looks like this:
$myMap = [];
foreach($data as $item) {
$myMap[$item['id']] = $item['disposable'];
}
It doesn't return anything when I try to echo my product with the ID "ADA001"
echo $myMap["ADA001"];
Also when I do "count($mymap)" it says its 2 records big, when it should be muuuch larger than that?
Thanks for help
I would use array_filter. Return the result of a comparitor.
$results = array_filter($targetArray, function($el) {
return $el === 1;
});
Edit: Now that it has been made clear that the OP wants to query from thousands of items, the correct way to do this is to make the Id the key to a map in PHP, like this:
$myMap = [];
foreach($array as $item) {
$myMap[$item['Id']] = $item['Stock'];
}
Now, whenever you want to access item '12', simply use $myMap['12'].
The reason this is faster is because of something called algorithmic complexity. You should read about Big-O notation for more info. Essentially, the first operation here is on the order of n and then looping through each of the items that comes out is on the order of n*log(n), so the final result is on the order of n*log(n) which is the best you'll be able to do without more information. However, if you were only accessing one element, just accessing that one element via MySQL would be better because it would be on the order of log(n), which is faster.
Edit 2: Also notice that if you were to access mutliple fields (ie not just the stock) you could do the following:
$myMap = [];
foreach($array as $item) {
$myMap[$item['Id']] = $item;
}
And simply access item 12's stock like this: $myMap['12']['stock'] or its name like this: $myMap['12']['name'].
You would do something like this.
$newArray=[];
foreach($array as $item){
if($item['Id'] === 1){
$newArray[] = $item;
}
}
$stockArray = array_column($newArray,'Stock');
My page grabs some records from a mssql DB, reads them into an array called $rows, with 3 columns:
ComputerName, Room, time_in_days
From $rows a second array is created (called $graph) with two columns, with the key being time_in_days from $rows, and the value column being a count of how often time_in_days occurred in the first array. The $graph array is used to create a number of HTML divs to give the impression of a graph.
I then want to be able to click on each individual div and using the $key value from the $graphs array, want to look up the rest of the information associated with all records in $rows where $graph[$key] matches $rows['time_in_days'] and display those records on the page.
But I have got stuck! I don't know where to put the function, the if statement(see code below) and at the moment, it doesn't even seem to be running the function. I don't even think I have the function code right. So if you could help with any of that I will very much appreciate it!
This is the code where the divs/graph is created:
foreach($graph as $key => $value){
$width = $value * $factor;
$page .= '<div style="width:40px;display:inline-block;">'.$key.'</div><div class="bar '.$key.'" style="width:'.$width.'px;"></div><div style="display:inline-block;margin-left:2px;">'.$value.'</div><br/>';
}
This is the function so far:
function SearchDays($key, $page, $rows) {
$computers = array();
foreach ($rows as $arr){
if ($key == $arr["time_in_days"]){
$computerName = $arr["ComputerName"];
$computers[$computerName] = $arr;
}
}
$page .= '<p>computers array from function SearchDays(): <pre>'.print_r($computers,true).'</pre></p>';
return;
}
And this is the if statement that makes the if statement that should run the function:
if (isset($_GET['barclick'])){
SearchDays($key, $page, $rows);
}
$page is just a variable that holds everything that is printed out onto the HTML page. The page itself can be seen here: cems.uwe.ac.uk/~s3-gupta/histogram/index.php. The whole page code can be got here: https://www.dropbox.com/s/h2q7x9xxtjbktx9/index.php
Thanks in advance. Please let me know if you need me to clarify things. I try and be as clear as possible on here, but usually don't manage it, so let me know if you need more.
I have a controller function in CodeIgniter that looks like this:
$perm = $this->job_m->getIdByGroup();
foreach($perm as $pe=>$p)
{
$pId = $p['id'];
$result = $this->job_m->getDatapermission($pId);
}
$data['permission'] = $result;
What I need to do is list the data in the result in the view, but I get only the last value while using this method. How can I pass all the results to the view?
Store it in an array. Like this:
foreach($perm as $pe=>$p){
$result[] = $this->job_m->getDatapermission($p['id']);
}
Because $result is not an array...
try this:
$result=array();
foreach($perm as $pe=>$p)
{
$pId = $p['id'];
$result[] = $this->job_m->getDatapermission($pId);
}
$data['permission'] = $result;
Note:
My answer uses a counter to enable the display of a single group result when needed.
Guessing from your need to loop and display the value of $result, possibly, it is an array or object returned by $query->result(). Things could be a bit complex.
Example: if $perm is an array of 5 items( or groups), the counter assigns keys 1 - 5 instead of 0 - 4 as would [] which could be misleading. Using the first view example, you could choose to display a single group value if you wants by passing it via a url segment. Making the code more flexible and reusable. E.g. You want to show just returns for group 2, in my example, $result[2] would do just that else next code runs. See my comments in the code.
$perm = $this->job_m->getIdByGroup();
$counter = 1;
foreach($perm as $pe=>$p)
{
$pId = $p['id'];
$result[$counter] = $this->job_m->getDatapermission($pId);
$counter++;
}
$data['permission'] = $result;
As mentioned above Note:
I Added a Counter or Key so you target specific level. If the groups are:
Men, Women, Boys, Girls, Children; you'd know women is group two(2) If you desire to display values for just that group, you don't need to rewrite the code below. Just pass the group key would be as easy as telling it by their sequence. To display all the loop without restrictions, use the second view example. To use both, use an if statement for that.
###To access it you could target a specific level like
if(isset($permission)){
foreach($permission[2] as $key => $value){
echo $value->columnname;
}
###To get all results:
foreach($permission as $array){
foreach($array as $key => $value){
echo $value->columnname;
}
}
}
Why am I not able to get inside the while loop in the getCustomers() function?
$stores = $bl->getStoresForGuide($gID); //Returns 6 stores
$storelist = getStoreList($stores); //Generate the HTML for the store list
$brandlist = getCustomers($stores); //Generate the HTML for brand list
function getStoreList($stores)
{
while ($row = mysql_fetch_array($stores)) {
// Do stuff
}
//return result
}
function getCustomers($stores)
{
echo mysql_num_rows($stores); //Outputs 6
while ($row = mysql_fetch_array($stores)) {
echo "test "; // Outputs nothing
}
// Return some result
}
You're looping twice. The first time you loop, you get to the end, and the pointer isn't reset before you loop again.
If you're sure you want to do this, check out mysql_data_seek, and seek to the beginning of the result. Otherwise, I'd recommend just storing the results and iterating over the array.
You are calling getStoreList first, then by the time you call getCustomers, $stores has already had all its rows fetched.