Jump to content

Change PHP query from URL string to CODE


ethans
Go to solution Solved by boompa,

Recommended Posts

Hello!

 

I'm hoping this makes sense.

Basically I have this code:

 

What it does

1) Looks at the XML

2) Looks for the PIN number in the URL, then finds it in the XML

3) Returns values for the PIN number (which is a profile) allowing me to do echo statements like name, tools, description, etc.

<?php
if (!empty($_GET['pin']))
{
$feedURL = 'http://www.inveroak.co.uk/readerimages/livepanel/91255.xml';
$showOpsWithNoPics = false;

try
{
$xml = simplexml_load_file($feedURL . '?' . time());
$readers = $xml->xpath('/ReaderDetails/Reader[Pin="' . $_GET['pin'] . '"]');

foreach ($readers as $reader)
{
    if ($reader->Picture == 'None' && $showOpsWithNoPics == false)
    {
        $picture = '';
    }
    elseif ($reader->Picture == 'None' && $showOpsWithNoPics == true)
    {
        $picture = 'images/defaultpic.jpg';
    }
    else
    {
        $picture = $reader->Picture;
    }

    if ($picture != '')
    {
        $name = $reader->Name;
        $pin = $reader->Pin;
        $status = $reader->Status;
        $description = $reader->Description;

        $arr_skills = '';
        $arr_subjects = '';
        foreach ($reader->Categories->Category as $Category)
        {
            foreach ($Category->Skill as $Skill)
            {
                switch ($Category['name'])
                {
                    case 'Skills':
                        $arr_skills .= "<li>$Skill</li>";
                        break;
                    case 'Subjects':
                        $arr_subjects .= "<li>$Skill</li>";
                        break;
                }
            }
        }

        $arr_shift = '';
        $hourWidth = 20;
        $PreviousDate = ""; 
        foreach ($reader->Rota->Shift as $Shift)
        {
            $Date = $Shift['Date'];
            $Day = date("l", strtotime($Date));
            $Start = $Shift['Start'];
            $Stop = $Shift['Stop'];
            if($Stop == '23:59:59' || $Stop == '00:00:00' ){$Stop = '24:00:00';}

            $Left = date("H",strtotime($Date.' '.$Start)) * $hourWidth;
            $Width = (round(abs(strtotime($Date.' '.$Stop) - strtotime($Date.' '.$Start)) / 60,2)/60)*$hourWidth;



            $compare = strcmp ($PreviousDate, $Date);


            if ($compare != 0)
            {
                if ($PreviousDate != '')
                {
                    //this is not the first loop so close off the previous dayrow and hours divs
                    $arr_shift.= '</div></div>';
                }
                //create a new day row
                $arr_shift .= '<div class="dayrow">';
                $arr_shift .= '<div class="day">'.$Day.'</div>';
                $arr_shift .= '<div class="hours">';
            }
            //add the shift object
            $arr_shift .= '<div class="shift" title="'. substr($Start,0,5).'-'.substr($Stop,0,5) .'" style="left:'.$Left.'px; width:'.$Width.'px; opacity: 1;"></div>';
            //set previouse date to the current shift's date so that we can check if we need to create a new day row next loop
            $PreviousDate = $Date;
        }
        //now we have finshed looping the shifts, either close the last div or display a message to show
        //we have no schedule for the op
        if ($Date != "")
{
            $arr_shift.= '</div></div>';
}
else
{
            $arr_shift.= 'We are sorry but they have not scheduled their hours this week. Email for further details.';
}
    }
}
}
catch (Exception $e)
{
echo 'cannot display operator profile';
}
}

?>

What I am want to achieve

1) Looks at the XML

2) Looks for a set PIN number in the CODE, then finds it in the XML

3) Returns values for the PIN number (which is a profile).

 
I've tried everything I can think of including the following:
 
$pin = $_GET['pin'];
// meeh I changed my mind
$pin = 4007;
// or depending on a test
$pin = $_GET['pin'] == 4007 ? 4007 : 0;

// further down the script
try
{
    $xml = simplexml_load_file($feedURL . '?' . time());
    $readers = $xml->xpath('/ReaderDetails/Reader[Pin="' . $pin . '"]');
} catch(Exception $e) {
    echo 'Cant find the profile. You sure the pin is right?';
}

and

$searchPin = 12345;
libxml_disable_entity_loader(true);
$xmlString = file_get_contents('http://www.inveroak.co.uk/readerimages/livepanel/91255.xml');
$xml = simplexml_load_file($xmlString);
$readers = $xml->xpath('/ReaderDetails/Reader/Pin[text()="' . $searchPin . '"]');

foreach($readers as $reader )
{
    print_r($reader->xpath('parent::*'));
}

but it's really not playing game.

 

I'm hoping someone here has the brain power to help me solve what I am guessing is something simple I am just not seeing!

 

If you need me to explain more what I mean, let me know! :)

Link to comment
Share on other sites

  • Solution

 

<?php
$xmlString = file_get_contents('http://www.inveroak.co.uk/readerimages/livepanel/91255.xml');
$xml = simplexml_load_string($xmlString); // load_string, not load_file
$readers = $xml->xpath("/ReaderDetails/Reader[Pin='4087']");
var_dump($readers);
Link to comment
Share on other sites

Hey!

 

That kind of works but just dumps all the information in the page.

 

It doesn't let me display their key bits where I want to:

 

Code


<?php

$xmlString = file_get_contents('http://www.inveroak.co.uk/readerimages/livepanel/91255.xml');
$xml = simplexml_load_string($xmlString); // load_string, not load_file
$readers = $xml->xpath("/ReaderDetails/Reader[Pin='4297']");
var_dump($readers);

if (!empty($_GET['pin']))
{
    $feedURL = 'http://www.inveroak.co.uk/readerimages/livepanel/91255.xml';
    $showOpsWithNoPics = false;

    try
    {
        $xml = simplexml_load_file($feedURL . '?' . time());
        $readers = $xml->xpath('/ReaderDetails/Reader[Pin="' . $_GET['pin'] . '"]');

        foreach ($readers as $reader)
        {
            if ($reader->Picture == 'None' && $showOpsWithNoPics == false)
            {
                $picture = '';
            }
            elseif ($reader->Picture == 'None' && $showOpsWithNoPics == true)
            {
                $picture = 'images/defaultpic.jpg';
            }
            else
            {
                $picture = $reader->Picture;
            }

            if ($picture != '')
            {
                $name = $reader->Name;
                $pin = $reader->Pin;
                $status = $reader->Status;
                $description = $reader->Description;
                
                $arr_skills = '';
                $arr_subjects = '';
                foreach ($reader->Categories->Category as $Category)
                {
                    foreach ($Category->Skill as $Skill)
                    {
                        switch ($Category['name'])
                        {
                            case 'Skills':
                                $arr_skills .= "<li>$Skill</li>";
                                break;
                            case 'Subjects':
                                $arr_subjects .= "<li>$Skill</li>";
                                break;
                            case 'Tools':
                                $arr_tools .= "<li>$Skill</li>";
                                break;
                            case 'Languages':
                                $arr_languages .= "<li>$Skill</li>";
                                break;
                        }
                    }
                }
                
                $arr_shift = '';
                $hourWidth = 20;
                $PreviousDate = ""; 
                foreach ($reader->Rota->Shift as $Shift)
                {
                    $Date = $Shift['Date'];
                    $Day = date("l", strtotime($Date));
                    $Start = $Shift['Start'];
                    $Stop = $Shift['Stop'];
                    if($Stop == '23:59:59' || $Stop == '00:00:00' ){$Stop = '24:00:00';}
                    
                    $Left = date("H",strtotime($Date.' '.$Start)) * $hourWidth;
                    $Width = (round(abs(strtotime($Date.' '.$Stop) - strtotime($Date.' '.$Start)) / 60,2)/60)*$hourWidth;
                    
                    
                    
                    $compare = strcmp ($PreviousDate, $Date);
                    
                    
                    if ($compare != 0)
                    {
                        if ($PreviousDate != '')
                        {
                            //this is not the first loop so close off the previous dayrow and hours divs
                            $arr_shift.= '</div></div>';
                        }
                        //create a new day row
                        $arr_shift .= '<div class="dayrow">';
                        $arr_shift .= '<div class="day">'.$Day.'</div>';
                        $arr_shift .= '<div class="hours">';
                    }
                    //add the shift object
                    $arr_shift .= '<div class="shift" title="'. substr($Start,0,5).'-'.substr($Stop,0,5) .'" style="left:'.$Left.'px; width:'.$Width.'px; opacity: 1;"></div>';
                    //set previouse date to the current shift's date so that we can check if we need to create a new day row next loop
                    $PreviousDate = $Date;
                }
                //now we have finshed looping the shifts, either close the last div or display a message to show
                //we have no schedule for the op
                if ($Date != "")
		{
                    $arr_shift.= '</div></div>';
		}
		else
		{
                    $arr_shift.= 'We are sorry but they have not scheduled their hours this week. Email customerservice@simplypsychic.com for further details.';
		}
            }
        }
    }
    catch (Exception $e)
    {
        echo 'cannot display operator profile';
    }
}

?>

<html>
<body>

<br><br>
Testing (name should be here): <?php echo $name; ?>

</body>

</html>

Looks like this:

 

4tqzhf.png

Link to comment
Share on other sites

It's okay, I fixed it! Thanks so much for your help.

 

This is the finished code for future reference if anyone else has same problem:

<?php

$xmlString = file_get_contents('http://www.inveroak.co.uk/readerimages/livepanel/91255.xml');
$xml = simplexml_load_string($xmlString); // load_string, not load_file
$readers = $xml->xpath("/ReaderDetails/Reader[Pin='4297']");


        foreach ($readers as $reader)
        {
            if ($reader->Picture == 'None' && $showOpsWithNoPics == false)
            {
                $picture = '';
            }
            elseif ($reader->Picture == 'None' && $showOpsWithNoPics == true)
            {
                $picture = 'images/defaultpic.jpg';
            }
            else
            {
                $picture = $reader->Picture;
            }

            if ($picture != '')
            {
                $name = $reader->Name;
                $pin = $reader->Pin;
                $status = $reader->Status;
                $description = $reader->Description;
                
                $arr_skills = '';
                $arr_subjects = '';
                foreach ($reader->Categories->Category as $Category)
                {
                    foreach ($Category->Skill as $Skill)
                    {
                        switch ($Category['name'])
                        {
                            case 'Skills':
                                $arr_skills .= "<li>$Skill</li>";
                                break;
                            case 'Subjects':
                                $arr_subjects .= "<li>$Skill</li>";
                                break;
                            case 'Tools':
                                $arr_tools .= "<li>$Skill</li>";
                                break;
                            case 'Languages':
                                $arr_languages .= "<li>$Skill</li>";
                                break;
                        }
                    }
                }
                
                $arr_shift = '';
                $hourWidth = 20;
                $PreviousDate = ""; 
                foreach ($reader->Rota->Shift as $Shift)
                {
                    $Date = $Shift['Date'];
                    $Day = date("l", strtotime($Date));
                    $Start = $Shift['Start'];
                    $Stop = $Shift['Stop'];
                    if($Stop == '23:59:59' || $Stop == '00:00:00' ){$Stop = '24:00:00';}
                    
                    $Left = date("H",strtotime($Date.' '.$Start)) * $hourWidth;
                    $Width = (round(abs(strtotime($Date.' '.$Stop) - strtotime($Date.' '.$Start)) / 60,2)/60)*$hourWidth;
                    
                    
                    
                    $compare = strcmp ($PreviousDate, $Date);
                    
                    
                    if ($compare != 0)
                    {
                        if ($PreviousDate != '')
                        {
                            //this is not the first loop so close off the previous dayrow and hours divs
                            $arr_shift.= '</div></div>';
                        }
                        //create a new day row
                        $arr_shift .= '<div class="dayrow">';
                        $arr_shift .= '<div class="day">'.$Day.'</div>';
                        $arr_shift .= '<div class="hours">';
                    }
                    //add the shift object
                    $arr_shift .= '<div class="shift" title="'. substr($Start,0,5).'-'.substr($Stop,0,5) .'" style="left:'.$Left.'px; width:'.$Width.'px; opacity: 1;"></div>';
                    //set previouse date to the current shift's date so that we can check if we need to create a new day row next loop
                    $PreviousDate = $Date;
                }
                //now we have finshed looping the shifts, either close the last div or display a message to show
                //we have no schedule for the op
                if ($Date != "")
		{
                    $arr_shift.= '</div></div>';
		}
		else
		{
                    $arr_shift.= 'We are sorry but they have not scheduled their hours this week. Email for further details.';
		}
            }
        }


?>

<html>
<body>

<br><br>
Testing (name should be here): <?php echo $name; ?>

</body>

</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.