Forum Moderators: coopster

Message Too Old, No Replies

Help passing data/parameters through include needed

         

JS_Harris

10:26 pm on Feb 7, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have a file named index.php that has a parameter change based on the subject of a page, I'll call the paremeter $ID. I have another file named datafile.php with a series of paragraphs of text and I want to include this file but only display the appropriate paragraph of text based on $ID of index.php. Unfortunately I can't simply put the contents of both pages together, datafile.php is somewhat lengthy and a database is not an alternative option.

index.php has
<?php
include ('datafile.php');
?>


datafile.php can be modified but something like the following does NOT work, obviously
<?php
if ($ID = "value1") {
echo "contents of paragraph 1";
}
elseif ($ID = "value2") {
echo "contents of paragraph 2";
}
?>


That's a simplistic version of the problem I'm trying to solve. I have the possible $ID's in an array on index.php and want to avoid a long elseif type list but am so far not finding an efficient way of only displaying the appropriate paragraph only based on $ID of index.php. Best guess, but I can't seem to get it working, is to just assign a parameter to each paragraph and then do the calculations on index.php, something like...

index.php
<?php
include('datafile.php');
if ($ID = "value1") {
echo $paragraph1;
} etc.
?>


Ideally this would all be in an array... but I seem to be missing something. I'm seeing the parameter ignored and all datafile.php contents are passed or none at all. What am I doing wrong?

[edited by: JS_Harris at 10:48 pm (utc) on Feb 7, 2016]

Selen

10:45 pm on Feb 7, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



Double equal sign, ie.

if ($ID == "value1")

JS_Harris

11:21 pm on Feb 7, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks, that didn't solve it however. index.php include is resulting in ALL text on datafile.php displaying on index.php regardless of parameters.

datafile.php contents
<?php
$para1 = 'some text';
$para2 = 'some extra text';
$para3 = 'some more text';
?>


index.php contains
<?php include './datafile.php';
echo $para1;
?>


result = "some textsome extra textsome more text" appearing on index.php. I cant get it to ONLY pull out the paragraph that matches. Also, the contents of datafile.php change and so I can't specify which specific lines to get.

Selen

11:23 pm on Feb 7, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



For comparison, double equal (if ($ID == "value1")).

datafile.php should have single equals, ie: $para1='some text'; $para2='some extra text'; etc.. it should work in theory ;)

Hm instead: include './datafile.php'; try:

require './datafile.php';

JS_Harris

11:42 pm on Feb 7, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Doesn't matter what I do the full text contents of datafile.php are being displayed simply by the include line without even asking for anything more, I need ONLY the section I want to have appear show up.

Include and require are doing the same thing. should an error occur, include will throw an error and require will cause a stop so I'd rather include if possible. How do I include ONLY the paragraph in datafile.php that I want? What I'm seeing doesn't seen to match what I'm reading should happen. I prefer databases to file reading for a reason... any other ideas on why include is spitting out the entire contents of datafile.php and not just the sections I want? It's not a cache issue, fyi, minor edits to datafile.php text are displayed instantly.

whitespace

11:55 pm on Feb 7, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



So, you can't get this to work at all? Or you can't get this to work "efficiently"?

Where is $ID being assigned in your first examples?

In your last example, result = "some textsome extra textsome more text" is impossible from the code you have posted - so there must be "something else" going on here?!

lucy24

12:56 am on Feb 8, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Disclaimer: As we all know, I only speak three words of php (and those three with a strong BASIC accent). But it makes me think of an issue I had, involving
onepage.php?blahblah
which had to include
otherpage.php?otherblahblah
and then there were actions based on the value of both blahblahs. I could only get it to work if I attached the entire query string to the outer file ("onepage.php") even though the required actions take place in the inner file ("otherpage.php").

Something tells me whitespace is now saying "Well, duhhh, how could you not have known that?"

JS_Harris

1:27 am on Feb 8, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



OK, so a 3rd file responsible for cache control was interfering with the process, I'm describing a simpler version of what I'm actually dealing with, my apologies. I am now able to pull the data from the proper paragraph in datafile.php to index.php by assigning a value to each paragraph in datafile.php. ie: $value1 = "para1 text"; etc

Yes, I was also asking about making it more efficient than a list of if, else, else way to parse datafile.php so...

I have all of the $ID values in an array I'll call $lock which is on index.php. It's not an associative array, it's used for other purposes to simply pass a yes or no (ie: in_array vs !in_array). Would modifying this array to contain both a key and a value work best? example: if (in_array($ID, $Lock, TRUE) && ? ) { ok, it's there, process.... if, for example, the $ID value is $value1 go get $para1 from datafile.php

Not sure how to most efficiently iterate through $lock to do this, and keep it all safe as well. I use a database for this type of thing usually.

JS_Harris

4:11 am on Feb 8, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Still can't get this right

Index.php contains
<?php
$ID = "ABC";
$Lock = array("ABC", "DEF", "GHI", "JKL", "ETC");
if (in_array($ID, $Lock, TRUE)) {
include("./datafile.php");
// proper paragraph from datafile.php needs to appear here
}
?>


datafile.php contains
<?php
$para1 = "paragraph of text related to ID ABC";
$para2 = "paragraph of text related to ID DEF";
?>


So, with the above, I would think I'd need to modify the array to create a link between "ABC" and $para1 and then modify it so that it gets the value instead of just checking to see if it's there. I've tried that several different ways but each time the text is not being drawn from datafile.php as expected. [php.net...] hasn't helped me much for this particular problem, yet.

Suggestions? The include path is fine for this example and $ID is ABC above but can be DEF or GHI etc depending on other factors, solving for ABC is fine.

Best effort: Since include is basically the same as having the content on the page I'll simplify this to one file
<?php
$ID = "ABC";
$Lock = array("ABC", "DEF", "GHI", "JKL", "ETC");
if (in_array($ID, $Lock, TRUE)) {
$para1 = "paragraph of text related to ID ABC";
$para2 = "paragraph of text related to ID DEF";
if ($ID == "ABC") echo $para1;
if ($ID == "DEF") echo $para2;
}
?>
// My brain is stuck on the fact that I already know what $ID is and so shouldn't need multiple if/else but navigating the include... /sigh


I mean it works but when you see the size of the content and properly add elseif and else statements, well, there has to be a cleaner way to do the above. None of my attempts at removing the "if ($ID..." and replacing it with an improved array has worked thus far.

whitespace

8:05 am on Feb 8, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



ASIDE: "otherpage.php?otherblahblah"
It's a valid point - and was actually my first thought when I read the thread title too. The include() includes a local filesystem file, it's not an HTTP request (by default), so there is no concept of a query string, it's just seen as an invalid filename, so the include fails to find the file. (Another "hacky" workaround might be to simply set $_GET['otherblahblah'] before including "otherpage.php".)

----

I'm a bit unsure which bits of your code you can change and which bits are part of your current structure and are unchangeable?

Anyway, working with your separate "datafile.php" example above, if you change the paragraph data to be held in an associative array, like so...


<?php
$data = Array();
$data['ABC'] = "paragraph of text related to ID ABC";
$data['DEF'] = "paragraph of text related to ID DEF";
?>


Then you can directly access the required paragraph (yes, no need for IF..ELSEIF construct) in index.php. For example:


<?php
$ID = "ABC";
$Lock = array("ABC", "DEF", "GHI", "JKL", "ETC");
if (in_array($ID, $Lock, TRUE)) {
include("./datafile.php");
// proper paragraph from datafile.php needs to appear here
if (isset($data[$ID])) {
echo $data[$ID];
} else {
// ERROR: No data for id $ID
}
}
?>

whitespace

8:13 am on Feb 8, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



If "paragraph of text" turns out to be several paragraphs and a whole bunch of formatting, then each block of text data could be stored in a separate file. eg. "datafile_ABC.html" (or .php), "datafile_DEF.html", etc. And then just include the appropriate datafile as required, for example:


include("data/datafile_$ID.html");


(You may want to check that the file exists first etc.)

JS_Harris

11:36 pm on Feb 8, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks whitespace, I had to +vote your first reply above because it is exactly what I needed. In looking over what I've tried I came close several times only to undo it and start over. File based database includes are fun.