I'm trying to make a news site (updating an existing one, really based on CMSMS, which has lots of autogenerated code that seems to cause all sorts of problems.) anyhow, I'm updating a template, where, instead of going to a new site where the article belonging to the resume is posted, I would like the resume to hide the div with the resume text in it at the click of a "read more" button. I know how to make the toggle button and all that, the issue is several resumes at the same page needing unique ID's that are set in the template automatically. The idea i had was to generate them with:
<?php $firstId = uniqid(); $secondId = uniqId(); ?>
Then I'd call them like so:
<div id="<?php echo $firstId; ?>" class=""> perhaps <span>here </span> or whatevs.</div>
And then something Similar for the second Id. The idea of doing it this way is that i'll be able to target these Divs from JS by simply aiming is at the $firstId fx.
however, if i rightclick and inspect the element with firebug i just get
id="<?php echo $firstId; ?>"
instead of:
id="someUniqueId"
So the question is how do i get it to actually parse the code so i can call the element by the ID? e.g. set its visibility to hidden.
I ended up using a for loop going through each element one by one, targeting them by their css classes, which in this case are just empty.
as i have access to control how many objects will be shown per page, as well as the templates of both full articles and summaries, i could use simple numbers with the certainty that they'd be unique each time.
The script simply goes through the page giving all the buttons within the div containing the elements i'm targeting, a value of 0-9. Then it gives the Divs containing the full articles an ID of the current button's ID + 40 resulting in them going from 40-49
and the summaries then going from 20-29
Then the rest of the code merely tagerts specific elements, using the button ID initially, then just subtracting/adding to the value in two different variables, to have the article id and the summary id, then i work my way from there. It works perfectly, even if it is a bit simple.
The inspiration came from your answer Fleshgrinder, thanks :)
Impossible to tell where the problem's solution is because it's a custom software. But the problem itself is easily identifier, the PHP is not parsed and executed. That's why you see the PHP opening and closing tags in the HTML output.
Using uniqid()
seems like a bad idea if you ask me, because you won't be able to directly target any of those elements in your JavaScript. Also the overhead of that function is totally unnecessary.
Simply use a counter for the elements which you initialize with an arbitrary number and increment it with each element you'll output.
Try considering this.
ob_start();
include_once "your_html_with_php_code_file";
$output = ob_get_contents();
ob_end_clean();
$output will be the thing you need.