-1

I want to use PHP only, no jquery to include a page if a div contains the word YES. The reason for this is so that I can use CMS to change the content inside a div from YES to NO to include a page or not. I have tried preg_match, but it searches either a variable or exact phrase in the PHP itself. This is what I am looking for:

<div id="available">YES</div>

<?php if (preg_match("/YES/i", ID="available")) {
include 'page.php';
} else {
echo "Not Available.";
}
?>

I need the ID="available" to search the div id="available" and if YES is found, include the page, if not, echo "Not Available." Thanks for any help.

AlwaysLearning
  • 197
  • 3
  • 5
  • 13
  • `if a div contains the word YES` Where? In the same PHP file or as a string? Where is this div tag located ? – Dave Chen Jan 23 '17 at 04:11
  • the div is one page1.php and I want to include page2.php if the div with the id="available" on page1.php contains the word YES. – AlwaysLearning Jan 23 '17 at 04:13
  • the code in the question would all be located on the same page, page1.php – AlwaysLearning Jan 23 '17 at 04:14
  • Can your CMS only change HTML? Why not just have the CMS change a variable in PHP (or better yet use a database or a text file to keep track). – Dave Chen Jan 23 '17 at 04:16
  • 1
    Can't you do this using DOM? – Akshay Jan 23 '17 at 04:18
  • CMS is third party, CushyCMS and it will not allow changing PHP – AlwaysLearning Jan 23 '17 at 04:19
  • 1
    Have a look [here](http://stackoverflow.com/a/8144104/5701450) – Akshay Jan 23 '17 at 04:21
  • sorry, but having a brain fart. i see the DOM to get the getElementsByTagName for the div id="available", how would I use that to search it for the word YES and then include the php page? – AlwaysLearning Jan 23 '17 at 04:27
  • if you cant change the php, how can this ever work? –  Jan 23 '17 at 04:45
  • If would have to be done with Ajax on a custom php file and overwrite the div on the fly. Personally I would prefer to go the opensource route and eliminate the need to hack together something like this. – CodingInTheUK Jan 23 '17 at 04:51
  • @ nogad - i can change the PHP in the code, just not the CMS. CMS is for HTML only, that is why I am looking at the PHP searchng at the div content instead of a variable. – AlwaysLearning Jan 23 '17 at 04:55
  • One question, where does the **YES** come from? is it hardcoded in the `page1.php` or it's a variable? I think if it's not hardcoded and you are on the fame file, then you can check the content of that variable! – EhsanT Jan 23 '17 at 04:58
  • YES is the text content inside of the div. – AlwaysLearning Jan 23 '17 at 05:09
  • I can see that, But what I'm asking is where does it come from?! – EhsanT Jan 23 '17 at 05:14
  • Let me clarify, when you edit your `page1.php`, You have exactly `
    YES
    ` or you have something like this: `
    $someVariable
    `. If you have case 1, it means that you always have **YES** in your div what so ever! if you have case 2, then you can check `$someVariable` to see if it contains **YES** or not!
    – EhsanT Jan 23 '17 at 05:17
  • @EhsanT - it is case1,
    YES
    which can be changed with CMS to say YES or NO. If YES, I want it to include page2.php
    – AlwaysLearning Jan 23 '17 at 05:31
  • So you are telling CMS actually edits `page1.php` and changes the content of it and saves it on the server?! and then you have some other code in the same `page1.php` which you want that code check the specific content of the same file for a value and do or do not do something base on that value? – EhsanT Jan 23 '17 at 05:35
  • @EhsanT - yes, the value of the div content – AlwaysLearning Jan 23 '17 at 05:45
  • 1
    Although I hardly can imagine a CMS would do that, But if that's the case, your best choice is using [php DOMDocument class](http://php.net/manual/en/class.domdocument.php) and [php DOMXpath class](http://php.net/manual/en/class.domxpath.php) – EhsanT Jan 23 '17 at 05:50

1 Answers1

0

I figured it out. This will allow you to edit your PHP variable with cushycms to include a page. If the div id="available" is anything other than Yes, the page2.php will not be included on page1.php. The code is attached below.

Page1.php

<h1>This is Page1.php </h1>
<?php
// locate page you want to check div on - page2.php
$content = file_get_contents('page2.php');
// input your specific div id from - page2.php
$first_step = explode( '<div id="available">' , $content );
$second_step = explode("</div>" , $first_step[1] );
// name your variable - in this case - $available
$available = trim($second_step[0]);
// input your output variable - $available and word to match in div - 'Yes'
if(false !== stripos($available, 'Yes')){
// if word in div=id'available' on page2.php is Yes - include page or echo
include 'page2.php';
}
?>

Note: on page2.php for this and cushycms to work, you have to put the div id="available" inside another div to have classes or styles - see example below

Page2.php

<div id="wrapper" class="cushycms">
<div id="available">Yes</div>
</div>
<h2>Please include this rental into the page</h2>
AlwaysLearning
  • 197
  • 3
  • 5
  • 13