I am trying to extract data from an XML file using simplexml. I am running a series of while and foreach loops to dig down to what I need.
However, I have found that after the elseif is finished I can no long display additional data to the screen (echo and print_r don't work). I have attached a code snippet for the elseif loop through the end of the end of the script. Please excuse the lousy code, I am a relatively new programmer.
elseif ($select_vars[1]==-1) {
$sub1=0;
$sub2=0;
$temp_count = 1;
$modnum=intval($select_vars[0]);
$sub1m = $xmls->module->{
intval($select_vars[0])}
->count();
while ($sub1 < $sub1m) {
foreach($xmls->module->{
$modnum}
->sublist1->{
$sub1}
as $hit2){
$sub2m = $xmls->module->{
$modnum}
->sublist1->{
$sub1}
->count();
while ($sub2 < $sub2m) {
foreach($xmls->module->{
$modnum}
->sublist1->{
$sub1}
->sublist2->{
$sub2}
as $hit3){
$template_string .= $temp_count . " - " . (string)$hit3 . "<br/>";
$temp_count = $temp_count+1;
$template_list[] = (string)$hit3;
}
$sub2 = $sub2+1;
}
$sub1 = $sub1+1;
$sub2 = 0;
}
}
echo "sub1 = " . $sub1 . "<br/>";
}
echo "here <br/><br/>".$template_string;
print_r ($template_list);
$template_list = array_unique($template_list);
sort($template_list);
print_r ($template_list);
I know the loops extract the right information because I can echo anywhere in the various loops except after the esleif closing bracket.
A live copy of the code is available at: http://www.aquilasolutions.us/software/templates/pages/filter-list.php
EDIT:
The output SHOULD be 3 selects and when the top box is filled when the others are empty a series of line (up to 400+) in the format ### - ####-####-####-#### should appear.
EDIT 2:
To View the error:
1. set select "Module " to be anything
2. Set select sublist 1 and sublist 2 to be empty
3. Make sure the array at the top has 3 keys (it represents a session cookie called filter-list and represents the selected items)
EDIT 3:
I have tracked down the error but not been able to resolve it.
There is a fatal error in my log (Fatal error: Call to a member function count() on null) on the line:
$sub2m = $xmls->module->{$modnum}->sublist1->{$sub1}->count();
I tried putting an isnull catch in but then there was a fatal error on the isnull....
I tracked down the issue and as expected it was my own stupidity!
I typed the wrong variable for the while ($sub2 < $sub2m) loop.
It SHOULD have been while ($sub2 < $sub2num)
Related
Hi all I have a question. I have an array that is dynamically populated. In the array there are 2 main types of items. Item with file name that equals 27 characters and the rest are either more or less. I am able to separate the 2 two types. The second list is added to a new array called $usedArray. Those items are then iterated through and the file name is substringed from character 0,6 to be used to compare the enduser's input on the page.
if the item is found in that array it will fire a function to send them a text and a email of the the full file name. my problem is if the item is not found until x iterations, it would fire the not found function x amount of times, and if it's not found at all it does the same thing. If I have 99 items that do not match it fire 99 times. to stop from firing I left the not found to just printing not found on the screen. I thought of calling the notfound function outside the loop but do not want it to fire if an items is found
This is my code I have so far
do{
if (substr($val,0,6) == $studentID)
{
$codeFound = substr($val,22,19);
print_r($studentID . ' is found <br /> Their code is ' . $codeFound);
//sendText($phoneNum,$codeFound,$messageMonth);
//sendEmail($emailInfo,$messageMonth,$codeFound);
break 1;
}
else
{
print_r($studentID . " was not found <br />");
}
} while(list(, $val) = each($usedArray));
This is my output
166003 was not found
166003 was not found
166003 was not found
166003 is found
Their code is xxxxxxxxxx
I think you should add a flag to track if you have found something or not:
$item_found = false;
do{
if (substr($val,0,6) == $studentID)
{
$codeFound = substr($val,22,19);
print_r($studentID . ' is found <br /> Their code is ' . $codeFound);
//sendText($phoneNum,$codeFound,$messageMonth);
//sendEmail($emailInfo,$messageMonth,$codeFound);
// item found!
$item_found = true;
break 1;
}
} while(list(, $val) = each($usedArray));
// now check - if `$item_found` is false
// then you can send your NotFoundEmail
if (!$item_found) {
sendNotFoundEmail();
}
I'm tying to extract data from thousands of premade sql files. I have a script that does what I need using the Mysqli driver in PHP, but it's really slow since it's one sql file at a time. I modified the script to create unique temp database names, which each sql file is loaded into. Data is extracted to an archive database table, then the temp database is dumped. In an effort to speed things up, I created a script structured 4 scripts similar to the one below, where each for loop is stored in it's own unique PHP file (the code below is only for a quick demo of what's going on in 4 separate files), they are setup to grab only 1/4 of the files from the source file folder. All of this works perfectly, the scripts run, there is zero interference with file handling. The issue is that I seem to get almost zero performance boost. Maybe 10 seconds faster :( I quickly refreshed my PHPmyadmin database listing page and could see the 4 different databases loaded at anytime, but I also noticed that it looked like it was still running more or less sequentially as the DB names were changing on the fly. I went the extra step of creating an unique user for each script with it's own connection. No improvement. Can I get this to work with mysqli / PHP or do I need to look into some other options? I'd prefer to do this all in PHP if I can (version 7.0). I tested by running the PHP scripts in my browser. Is that the issue? I haven't written any code to execute them on the command line and set them to the background yet. One last note, all the users in my mysql database have no limits on connections, etc.
$numbers = array('0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20');
$numCount = count($numbers);
$a = '0';
$b = '1';
$c = '2';
$d = '3';
$rebuild = array();
echo"<br>";
for($a; $a <= $numCount; $a+=4){
if(array_key_exists($a, $numbers)){
echo $numbers[$a]."<br>";
}
}
echo "<br>";
for($b; $b <= $numCount; $b+=4){
if(array_key_exists($b, $numbers)){
echo $numbers[$b]."<br>";
}
}
echo "<br>";
for($c; $c <= $numCount; $c+=4){
if(array_key_exists($c, $numbers)){
echo $numbers[$c]."<br>";
}
}
echo "<br>";
for($d; $d <= $numCount; $d+=4){
if(array_key_exists($d, $numbers)){
echo $numbers[$d]."<br>";
}
}
Try this:
<?php
class BackgroundTask extends Thread {
public $output;
protected $input;
public function run() {
/* Processing here, use $output for... well... outputting data */
// Here you would implement your for() loops, for example, using $this->input as their data
// Some dumb value to demonstrate
$output = "SOME DATA!";
}
function __construct($input_data) {
$this->input = $input_data;
}
}
// Create instances with different input data
// Each "quarter" will be a quarter of your data, as you're trying to do right now
$job1 = new BackgroundTask($first_quarter);
$job1->start();
$job2 = new BackgroundTask($second_quarter);
$job2->start();
$job3 = new BackgroundTask($third_quarter);
$job3->start();
$job4 = new BackgroundTask($fourth_quarter);
$job4->start();
// ==================
// "join" the first job, i.e. "wait until it's finished"
$job1->join();
echo "First output: " . $job1->output;
$job2->join();
echo "Second output: " . $job2->output;
$job3->join();
echo "Third output: " . $job3->output;
$job4->join();
echo "Fourth output: " . $job4->output;
?>
When using four calls to your own script through HTTP, you're overloading your connections for no useful reason. Instead, you're taking away spaces for other users who may be trying to access your website.
Summary:
I have an app with a slew of checkboxes on the page.
The user selects whatever checkboxes they want, and hits submit.
I need to do some conflict checking.
My current state of the code is working great.. however I discovered a flaw in the logic it seems, that allows for certain 'selections' to pass through without being noted as a conflict. (but I'm not clear as to WHY they are slipping past, nor how to fix it)
Technical Overview:
I take an array of the submitted controls/elements, and loop through each one doing the following:
1.) checkStartFirst() = See what one starts first and arrange the order of the two 'times' as arguments sent to another function (isConflict())
2.) isConflict() = takes the arguments passed to it..and checks to see if the argumentX start time is greater than argumentY's end time.
I then push these values into another array to be used later one.
3.) After the above loop is complete.. I have another function that clears out any dups in the array and passes it back where I need it to be (to be used in front end for highlighting said conflicts)
debugging output for 1 of the questionable selections that slips past and doesnt get marked as a conflict, (where other same date/time selection DO get flagged as a conflict)
Sending: hours_9_7_reg_session_101_945_1005 /
hours_9_7_reg_session_102_945_1005
Received for checking: hours_9_7_reg_session_101_945_1005 /
hours_9_7_reg_session_102_945_1005
B: CHECKING: hours_9_7_reg_session_102_945_1005 [end: 09/07/2015
10:05] > hours_9_7_reg_session_101_945_1005 [start: 09/07/2015 9:45]
Since it is in a loop, it gets checked again (other way)
Sending: hours_9_7_reg_session_102_925_945 /
hours_9_7_reg_session_101_945_1005
Received for checking: hours_9_7_reg_session_102_925_945 /
hours_9_7_reg_session_101_945_1005
A: CHECKING: hours_9_7_reg_session_102_925_945 [end: 09/07/2015 9:45] >
hours_9_7_reg_session_101_945_1005 [start: 09/07/2015 9:45]
(again, slips past)
My PHP functions.. starts with calling setConflicts()
//new (proposed) session conflicts checker
function checkStartFirst($cf_presX, $cf_presY) {
//$cf_presX['fullStart'] < $cf_presY['fullStart'] ? $this->isConflict($cf_presX, $cf_presY) : $this->isConflict($cf_presY, $cf_presX);
echo 'Received for checking: '. $cf_presX['id'] . ' / ' . $cf_presY['id'] .'<br>';
if($cf_presX['fullStart'] < $cf_presY['fullStart']){
echo 'A: ';
$this->isConflict($cf_presX, $cf_presY);
}else{
echo 'B: ';
$this->isConflict($cf_presY, $cf_presX);
}
}
function isConflict ($cc_presX, $cc_presY) {
echo 'CHECKING: ' . $cc_presX['id'] .' [end: ' . $cc_presX['fullEnd'] . '] > ' . $cc_presY['id'] .' [start: ' . $cc_presY['fullStart'] . ']';
if ($cc_presX['fullEnd'] > $cc_presY['fullStart']) {
echo ' -- has conflict <br>';
array_push($this->conflict_output, $cc_presX['id']);
array_push($this->conflict_output, $cc_presY['id']);
//echo 'Found Conflict: ' . $cc_presX['id'] . ' / ' . $cc_presY['id'] . '<br>';
}else{
//only here for debugging readability
echo '<br>';
}
}
function setConflicts() {
$presentations = $this->conflict_list;
for ($i = 0; $i < count($presentations); $i++) {
for ($j = 0; $j < count($presentations); $j++) {
// if it is not the same event
if ($presentations[$i]['id'] != $presentations[$j]['id']) {
echo '<br><br>Sending: '.($presentations[$i]['id'] .' / '. $presentations[$j]['id']) .'<br>';
$this->checkStartFirst($presentations[$i], $presentations[$j]);
}else{
echo '<br><br><br>same session, do not check: ' . ($presentations[$i]['id'] .' / '. $presentations[$j]['id']) . '<br><br><br>';
}
}
}
$this->getConflicts();
}
function getConflicts(){
$presentations = $this->conflict_output;
//remove duplicates in array & re-key (sequentially)
$uniquePresentations = array_unique($presentations);
//re-key array using sequential index #'s
$uniquePresentations = array_values($uniquePresentations);
if(count($uniquePresentations) > 0){
//save conflict (names) to array
$this->conflict_return = $uniquePresentations;
$this->errmsg = 'Please review the form for errors, there are conflicts in the highlighted sessions below. (Possible duplicate or overlapping session times have been selected.) <br>You can not proceed until all conflicts are resolved.';
}
}
How can the code/time selections in the blockquote above be slipping past? Where as others -do- get flagged?
I'm not sure if I need to tighten up some conditionals or something?
Well the solution (error) was easier than I expected.
Basically anywhere I am checking the date/time stamps.. I need to make sure I am casting/converting them to a strtotme() value ... as pulling the value out of the object/array it is treated as a string.
Why every other time conflict checking worked without issue.. I dont know. LOL
But doing this fixed my problems:
if(strtotime($cf_presX['fullStart']) < strtotime($cf_presY['fullStart'])){
if (strtotime($cc_presX['fullEnd']) > strtotime($cc_presY['fullStart'])) {
I'm having a bit of a brain-fart here...lol
I was using a forloop() initially for this.. but then the conditions got a bit more advanced on things I needed to check for, as well as the output based on found/not found..
My problem is ensuring that on consecutive loops.. the 'else' on the (no access) potion only gets echo'd once... but of course on every 'loop' it will check from the top and if not a match output the 'no-access' text.. (on every iteration).. where it should only output once.
Originally there was only a few if() statement/checks in the foreach() loop.. where a simple break; took care of things fine...
but these if()'s turned into if/else.. which means the else will get 'triggered' on the next 'loop'.. how can this be prevented?
$arrwebinars = ("Name1", "Name3");
foreach($arrwebinars as $webinar) {
/* Webinar 1 */
if($webinar == 'Name1') {
if($web01_title != '') {
echo "available";
} else {
echo "not available";
}
} else {
echo "no access";
}
/* Webinar 2 */
if ($webinar == 'Name2') {
if ($web02_title != '') {
echo "available";
} else {
echo "not available";
}
} else {
echo "no access";
}
/* Webinar 3 */
if ($webinar == 'Name3') {
if($web03_title != '') {
echo "available";
} else {
echo "not available";
}
} else {
echo "no access";
}
}
is there some other sort of 'control' I can use to ensure the main if/else only gets executed once?
Sorry , I am having a hard time trying to describe this one, hopefully the code explains it all (and the problem)
thanks.
update:
Current State: reverted back to foreach() loop.
User is only authorized for 2 vids (#5 & #6 we'll say..can be any of the 6 in reality)
Upon first loop/iteration.. vids 1-4 output (no access for you!) (because they dont, only #5 & #6 as stated above)
no. 5 outputs the embed code fine (access)
no. 6 says 'No access for you'.. (even though they do/should have access)
Upon the scond iteration..vids 1-4 are duplicated, and again say "No access for you" (outside of it being a duplication..this is correct).. however, vid #5 NOW says "no access for you" (first loop it outputted fine, second loop it is looking for a new 'match' while not what I want.. in theory this is correct.. but should have a duplicate)
Vid #6 is NOW outputting the embed code (where as on first loop it did not)..
The user has only purchased access to 2 vids.. so the loop only happens twice.
but I get '12' echo's
No matter what I should only get 6 echo's with 1 out put per video (available/not available -or- no access)
I cant seem to wrap my head around this today.. :(
Goal I want achieve:
6 outputs TOTAL
each output (echo) should ONLY have:
available or not available printed (this is the nested conditional check for the blank title)
-or-
no access
there are 6 video to check against.
the user can have purchased access to 1 or up to all 6 vids
first check is: is the webinar name in their purchased array found in the total available 6 vids available
second tier conditional check:
if YES (match found == access)...then check to see if the title is missing (if there output access [video embed code]... if not there output text 'not available yet')
(going back to first conditional check) if there is NO ACCESS (match not found).. output 'no access' text.
This works if I wanted duplicates on the stage/page.. (but I dont!) LOL..
I need to be limited to ONLY 6 outputs,.. as there are only a total of 6 vids available.
I DO NOT WANT:
on loop 1 it outputs access for vid#1 and #2-#6 are no access..
on loop 2 it re-states all of these outputs, has vid#1 no-access now, vid#2 has access and vids #3-#6 are no access...
etc.etc.. and the cycle continues. I'm getting lost on this one!..
thanks!
If you wish to stop considering that "row" when you encounter a "no access" then you could simply continue; after each no-access. or do you wish to check the others even if you encounter "no access" on the first, just to fail silently in that case?
Not sure exactly what you're trying to achieve, but your code could be shortened greatly.
$webinars = array('one', 'two', 'Name3');
$web0_title = 'not empty'; // Just a demo
for ( $i = 0; $i < count($webinars); $i++ )
{
$access = FALSE;
$available = FALSE;
$name = 'Name' . $i;
$title = 'web' . $i . '_title';
if ( $webinars[$i] == $name ) {
if ( ! empty( $$title ) ) {
$available = TRUE;
}
$access = TRUE;
}
// Simple debug, not sure what you want to accomplish
echo 'Webinar: ' . $webinars[$i] . ' <br />';
echo 'Availability: ' . ( $available ? 'Available' : 'Not Available' ) . '<br />';
echo 'Access: ' . ( $access ? 'Access' : 'No Access' ) . '<br /><br />';
}
Edit: Just read your comments so I updated it.
I am trying to retrieve data from a json feed and display using PHP. Only issue is that some elements will return back empty. As I have this in a loop I want to return each post, however not all have a picture, message or comments and comes back as Notice: Undefined variable. (I am trying to retrieve data from a fb group)
Here are the settings
<?php
// Settings
$groupID = "231809390188650";
$accessToken = "myTokenGoesHere";
// Request and parse json
$json_string = file_get_contents("https://graph.facebook.com/$groupID/feed? access_token=$accessToken");
$parsed_json = json_decode($json_string);
for ($i = 0; $i < $json_string; $i++) {
// Returned data
$gImage = $parsed_json->data[i]->picture;
$gMessage = $parsed_json->data[i]->message;
$gCreated_time = $parsed_json->data[i]->created_time;
$gUpdated_time = $parsed_json->data[i]->updated_time;
$gComments_message = $parsed_json->data[i]->comments->data[i]->message;
};
?>
Here is the output (rough example)
<h1> Facebook Group </h1>
<?php
echo "IMAGE URL : " , $gImage;
echo "MESSAGE : " , $gMessage;
echo "DATE POSTED : " , $gCreated_time;
echo "UPDATED AT : " , $gUpdated_time;
echo "COMMENTS: ", $gComments_message;
?>
With this I get Notice: Undefined variable: gImage etc for each one. I'm not sure why this is happening. Here is an example of the json feed which shows that not all elements will have something in them. http://pastebin.com/eUJce5VT
The problem is that those elements do not always exist, and you have E_STRICT error reporting turned on in PHP (which actually helps you spot issues).
Thus, you need to make sure that all variables are actually set before trying to output them. Also, make sure to access all variables with a $ prefix. That might look like this:
for ($i = 0; $i < $json_string; $i++) {
$gImage = $parsed_json->data[$i]->picture;
$gMessage = $parsed_json->data[$i]->message;
$gCreated_time = $parsed_json->data[$i]->created_time;
$gUpdated_time = $parsed_json->data[$i]->updated_time;
if (isset($gImage))
echo "IMAGE URL : " , $gImage;
if (isset($gMessage))
echo "MESSAGE : " , $gMessage;
if (isset($gCreated_time))
echo "DATE POSTED : " , $gCreated_time;
if (isset($gUpdated_time))
echo "UPDATED AT : " , $gUpdated_time;
}
Please note that I removed the Comments variable. You'll need a separate loop inside the first one to access all the comments.