Im trying to get dynamic with php. And I wonder if this is possible. However i pass a $var to ID the section does not show up. Lets say I have this:
<?php
$var = "welcome";
$html = '<section id="'.$var.'">Im glad you are here</section>';
echo $html;
?>
So this is the basic thing and I cant find any other reference to this. Note: Im writing this from my phone so forgive me if there is a syntax error.
Thanks in advance and wish you a good day.
Well the first thing I see is there is a mismatch between your variable name and the variable you pass.
Variable decaration is as V
then you pass var
Related
I'm a total noob on php, but a friend of mine asked for help and I thought I might do it..
I have this code/file here and want to find a way to add an element in this array via html file. I know it sounds noob, it does for me too, but please help, I've seen arrays, vectors and lists only on c++, tried to take a look at the documentation of php5 (since he want it in php 5) but I couldn't make it!
here it is...
<?php
$bledi = array('user12345', 'user2016', 'user5749852', 'user985658', 'HowToAddANewElement');
echo $result;
?>
You need a form with a text input in HTML and a send button.
When the form is submitted to the action page there you could put
your php.
You can make a global array ($bledi) and you will have an if condition there that says something like
if($_REQUEST['input_text'])
$bledi[] = $_REQUEST['input_text'];
I'm trying to create a link that takes the user to two different pages depending is the user logged or not. Problem is I'm still new to programming and this is quite big bite for beginner like me but its something I have to do. I created something like this so far but either way I suck at searching or there just isnt specific information for what I need
<?php if($userLogged){
echo '<a href="index.php" class="stylelink">';
}
else
{
echo '<a href="index1.php" class="stylelink">';
}
echo "Etusivu</a>";
?>
I'm also using Dreamweaver's login function that creates the MM_Username session and such, and Im not sure how to make the condition. userLogged is still an empty variable. Id appreciate any advice.
Thanks
-John
well, instead of using echo statements in the php tag you can write html and use php for outputting the value of the page like this
Etusivu
The $_SESSION['MM_Username'] works if you have included session_start(); at the beginning of the page and you can use the condition as above instead of $userLogged.
can i set the id of an element programatically, as in a dynamic setting of id of an element, during runtime of the webpage?
Consider this scenario -
<body id="body1">
Now, i want to set the id of this element "body" dynamically, using the value of a variable in the php code. Is this possible? something like -
<body id=" <?php echo $bodyID; ?> ">
I use php and jquery in the page; please let me know whether such dynamic id assignment is possible to the elements in the html.
thanks.
PHP is the background language, it's what produces your HTML output. So basically, what ever you output from PHP eventually becomes your HTML, meaning yes you can use PHP variables as your elemnt-ID or anything else for that matter.
<?PHP
$var = "body1";
?>
<body id="<?PHP echo $var; ?>"> Hello my ID is <?PHP echo $var; ?></body>
OR You can output all of the HTML using a single echo statement.
<?PHP
$var = "body1";
echo "<body id='$var'>Hello my ID is $var</body>";
?>
In conclusion, whatever is left after PHP is finished executing is your HTML code that the end users browser interprets... Hope this helps...
$(function() {
var variable = 'insert_id';
$('body').attr('id', variable);
});
gives (with jquery)
<body id="insert_id">
Why not? As long as you keep your randomizer ( Math.rand ) big enough there should be little chance for conflicts.
I usually do this, and then at the same time call a JS method and passing the same ID. That would require you storing the ID aside so you can pass it later.
Edit: If you are only setting this on the body then you would not need to access it later.
here is my code
<div id="lara">some crap</div>
<div id="jerk">some crap</div>
<div id="yessir">some crap</div>
basically i'm wondering if there is a way to replace the filename (of the image) with some php that will dynamically add the id of the parent div in its place.
why you ask? i have several links like this where the id matches the filename, and i feel as though i should not have to manually type each file name in. I believe i've seen this done in ASP, but am not sure what the method is called, or if it's possible in php.
help!
I hope I understood you:
$arr = array("foo", "foo1", "foo2");
foreach($foo as $item){
echo "<div id='$item'><a href='www.something.com/$item'>$item</a></div>";
}
The quickest way I could think if would be to make it into a function..
function printDiv($name) {
echo "<div id='$name'><a href='http://anywhere.com/work/$name.jpg'>some crap</a></div>";
}
To call it, do the following:
printDiv('lara');
You might want to modify this to return a string instead of echoing depending on what you're doing..
The only way you can do this in PHP is if you have the ids in a variable in PHP.
<div id="<?=$name?>">some crap
Otherwise you will have to do it in Javascript, maybe with something like jQuery: http://jquery.com/
This is a continuation of an earlier post. Unfortunately, The solutions posted didn't work and my follow up questions weren't addressed. Just in case this is because my generous respondents didn't notice I had replied, I'm reposting my problem.
I'm trying to build a form where certain text fields and text areas have autocomplete.
I've used the formidable plugin for wordpress to build my form. I'm using the jQuery autocomplete plugin for the autocomplete part.
After implementing the suggestions of one of my respondents, the code now looks like this:
<script>
$(document).ready(function(){
<?php global $frm_entry_meta;
$entries = $frm_entry_meta->get_entry_metas_for_field(37, $order=''); ?>
var data = new Array(); <?php foreach($entries as $value){ ?>
data.push(unescape('<?php echo rawurlencode($value); ?>');
<?php } ?>
$("#field_name-of-the-school").autocomplete(data); })
</script>
// 37 is the field id I want to pull from in the database,
// and #field_name-of-the-school is the css id
// for the text field in the form where a user can enter the name of a school.
// the data for that field is stored in the db under field id 37.
// other fields, like school id, have their own field ids.
My respondent suggested adding the data.push(unescape('<?php echo rawurlencode($value); ?>'); bit. Unfortunately, it's still not working.
BTW, the code is in the body of page.php, a template which wordpress uses to generate static pages (the form is on one of these).
I would seriously, seriously appreciate any and all help with this. If this approach is a dead end (on the earlier posts, there were two other answers that were over my head,) I would be more than willing to learn some new tricks (though it would really help to be pointed to a relevant learning resource.)
Thanks in advance.
I would jsut use json_encode and simplify it:
<script>
$(document).ready(function(){
<?php global $frm_entry_meta;
$entries = $frm_entry_meta->get_entry_metas_for_field(37, $order=''); ?>
var data = <?php echo json_encode($entries); ?>;
$("#field_name-of-the-school").autocomplete({source: data});
}); // note you were missing a semicolon at the end of this, which i added
</script>
Of course using the above may not be waht you want if $entries is an associative array instead of a numeric indexed one. If thats the case you can do json_encode(array_values($entries));
You have to use
$("#field_name-of-the-school").autocomplete({ source : data });
as shown in the Autocomplete documentation example. Also you might think
about using JSON.