Could anybody help me with this little issue. Im trying to change the class of a page number if clicked. but im trying through a js function call using a dynamic php ID . The code below should show you what i mean, Basically the function would change the class of the selected number.
echo"
<div class='pagebuttonsdiv'>";
for ( $x =0 ; $x < $pagesnum ; $x++ ) {
$pagex=$x+1;
if ($pagex<1) {$pagex==1;}
if ($pagex>$pagesnum) {$pagex==$pagesnum;};
if ($x > $pagesnum) {$x==$pagesnum;}
echo "
<div id='{$x}' class='pagebuttons' onclick='pageclick({$x},
{$display}, {$maxresults} , {$startfrom});'>
{$pagex}</div>";
if ($x==$ifclicked) {?>
<script type="text/javascript">
document.getElementById({$x}).className =
'pagebuttonsclicked';}
</script>
<?php
};
};
echo"
</div>
";
Anybody have any idea how i should go about this, Many thanks in advance.
<div id="id[<?php echo $x; ?>]" class="pagebuttons" onclick="pageclick(<?php echo$x; ?>)">
{$pagex}</div>
Not sure why are you sending all those variables in function. you just need the id to change that.
Simple Answer -
echo"
";
for ( $x =0 ; $x < $pagesnum ; $x++ ) {
$pagex=$x+1;
if ($pagex<1) {$pagex==1;}
if ($pagex>$pagesnum) {$pagex==$pagesnum;};
if ($x > $pagesnum) {$x==$pagesnum;}
echo "
<div id='{$x}' class='pagebuttons' onclick='pageclick({$x},{$display}, {$maxresults} , {$startfrom});'>
{$pagex}</div>";
if ($x==$ifclicked) { echo " <script> document.getElementById({$x}).className='pagebuttonsclicked' </script> ";};
};
echo"
</div>
<br>
";
call the function from php to change class. = onlclicking the number, it changes its class to change whatever you wish it to change according to the new css class... I changed the border surround color...
Related
i have more then 5 link for image, so i want to make like this,
$p_image1, $p_image2, $p_image3, $p_image4, $p_image5
but i dont understand why my cod is not work...
this is my code for get array data:
$id = mysqli_real_escape_string($koneksi,$_GET['i']);
$query = mysqli_query($koneksi,"SELECT * FROM `tb_produk` WHERE `p_id` = '$id'");
$get = mysqli_fetch_array($query);
this is my code for loop:
if ($j_image > 1) {
for ($i = 1; $i <= $j_image; $i++) {
$p_image[] = $get['p_image'.$i];
if ($i > 4) {
break;
}
?>
<li data-uk-slideshow-item="<?php echo $i ?>">
<img src="<?php echo $get['p_image'.$i]; ?>">
</li>
<?php
}
}
?>
why this is not work, thanks for your help before :)
I think creating dynamic named variables is just a way to make things harder than it is.
Instead I believe fixing the problem is the solution.
Dynamic variables is just harder to work with than arrays and will make bugs in your code some day.
Here I use a foreach on the $get array which means it will loop the items there is and we don't have to count and guess stuff.
Then I removed this new array you created as I don't see the point of it and instead go directly to the output part with my foreach variable $image.
I also keep the code in PHP and echo the html as I find that easier to read, but that is purely opinion and you can do whichever you want.
$i=1;
foreach($get as $image){
if ($i > 4) {
break;
}
echo "<li data-uk-slideshow-item=" . $i .">\n";
echo ' <img src="' . $image . '">' . "\n";
echo "</li>\n";
$i++;
}
Example output:
<li data-uk-slideshow-item=1>
<img src="1">
</li>
<li data-uk-slideshow-item=2>
<img src="2">
</li>
<li data-uk-slideshow-item=3>
<img src="3">
</li>
<li data-uk-slideshow-item=4>
<img src="4">
</li>
Not sure if your code is supposed to output four or five items.
But I just left the if and break as it was in your code.
https://3v4l.org/MrAng
Array is the perfect way to handle multiple similar values. You should really not consider making separate variables for this.
Still, in order to create dynamic variables, you need to use an additional $, and wrap them in curly braces {..}:
for ($i = 1; $i <= $j_image; $i++) {
// Create string for dynamic variable name and put it inside curly braces
// use $ in front to define this as a new variable
${'p_image'.$i} = $get['p_image'.$i];
if ($i > 4) {
break;
}
I was hoping somebody could help me with this. I'm using codeigniter as my framework. In one of my views, I'd like to output as many tabs as the number in "bestoutof" column in my database. The code I tried is:
<?php for ($x = 1; $x = $post->bestoutof; $x++) {
echo '<div class="tab-pane fade active in" id="game<?php $post->bestoutof; ?>"></div>'; } ?>
But it doesn't seem to work. I'd greatly appreciate any help on this.
Thank you!
For Id inside the for loop you need to use $x as like this example:
Modified code:
<?php
for ($x = 1; $x = $post['bestoutof']; $x++) {
?>
<div class="tab-pane fade active in" id="game<?php echo $x;?>"></div>
<?php
}
?>
Note: If $post['bestoutof'] have multiple values, then $x = $post['bestoutof'] value should be added less-than mark $x <= $post['bestoutof']. This will depend with what the value you have on it
What's wrong in your code you are using $post->bestoutof for every iteration. This will return you same game Id not changed.
And do not use php tag inside the php tag.
You have PHP tags inside PHP tags. You're going to get your code output as a literal string. As #devpro pointed out, you probably want to use the counter for your id otherwise, you'll have duplicate ids.
<?php for ($x = 1; $x = $post->bestoutof; $x++) {
echo '<div class="tab-pane fade active in" id="game'. $x .'"></div>';
} ?>
$x = $post->bestoutof; This decides the number of iteration.
Actually it should be $x <= $post->bestoutof;
<?php for ($x = 1; $x <= $post->bestoutof; $x++) {
echo '<div class="tab-pane fade active in" id="game<?php $post->bestoutof; ?>"></div>'; } ?>
I am creating Hang a Man using PHP, MySQL & Javascript. Every thing is going perfect, I get a word randomly from DB show it as a label apply it a class where display = none. Now when I click on a Character that character become disable fine which i actually want but the label-character does not show.
My code is:
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
<?php
include( 'config.php' );
$question = questions(); // Get question.
$alpha = alphabats(); // Get alphabets.
?>
<script language="javascript">
function clickMe( name ){
var question = '<?php echo $question; ?>';
var questionLen = <?php echo strlen($question); ?>;
for ( var i = 0; i < questionLen; i++ ){
if ( question[i] == name ){
var link = document.getElementById( name );
link.style.display = 'none';
var label = document.getElementById( 'questionLabel' + i );
label.style.display = 'block';
}
}
}
</script>
<div>
<table align="center" style="border:solid 1px">
<tr>
<?php
for ( $i = 0; $i < 26; $i++ ) {
echo "<td><a href='#' id=$alpha[$i] name=$alpha[$i] onclick=clickMe('$alpha[$i]');>". $alpha[$i] ."</a> </td>";
}
?>
</tr>
</table>
<br/>
<table align="center" style="border:solid 1px">
<tr>
<?php
for ( $i = 0; $i < strlen($question); $i++ ) {
echo "<td class='question'><label id=questionLabel$i >". $question[$i] ."</label></td>";
}
?>
</tr>
</table>
</div>
First of all, why would it show, when you're setting its display to none?
Second, you probably want to take the hiding of the letter outside the if - if you don't, you'll be hiding the letter several times over if it appears couple of times in the question (think "banana" - if you pick "a", it will hide "a" three times) - which is not an issue, and won't hide the letter if it does not appear in the question - which probably is.
Third - Why are you using labels? You can, it's not illegal or anything, but they have a clear purpose - to mark text belonging to checkboxes and other selectable elements that don't have text of their own. It is best to use elements according to their intended meaning. As there is no HTML element dedicated to single letters in a hangman game, you're best off with span or div.
UPDATE:
Try this; I'm not sure, but reasonably convinced that this is what you want:
for ( var i = 0; i < questionLen; i++ ){
var link = document.getElementById( name );
link.style.display = 'none';
if ( question[i] == name ){
var label = document.getElementById( 'questionLabel' + i );
label.style.display = 'inline';
}
}
Have you tried label.style.display = ''; instead of 'block'?
$question seems to have been mis used...
on this lines:
for ( $i = 0; $i < strlen($question); $i++ ) {
echo "<td class='question'><label id=questionLabel$i >". $question[$i] ."</label></td>";
}
you say strlen which is the number of characters in a string. or aka string length.
and then you say "....$question[$i]...." which is a non array ...
so....
replace "strlen" with "count" and then use str_split on $questions.
so you end up with ...
$question = str_split($question);
for ( $i = 0; $i < count($question); $i++ ) {
echo "<td class='question'><label id=questionLabel$i >". $question[$i] ."</label></td>";
}
this would split each character which is what i think you are trying to do.
The problem (as Amadan pointed out) is that you're setting the display to none for the label (looks like you might have copy-pasted):
var link = document.getElementById( name );
link.style.display = 'none';
var label = document.getElementById( 'questionLabel' + i );
label.style.display = 'inline';
Also, you might consider refactoring to use a regular expression instead of looping through the string:
function clickMe(name) {
// Get the question string
var question = '<?php echo $question; ?>',
// Create a RegExp based on the name
re = new RegExp(name, "gi"),
// Get a handle to the link
link = document.getElementById(name),
// Set up our `match` variable
match;
// Set the link display to "none" outside of the loop
link.style.display = "none";
// For each match found in the question, show that label.
while(match = re.exec(question))
document.getElementById("questionLabel"+match.index)
.style.display = "inline";
}
Your function compresses down to only 7 lines of code this way, making it easier to read and a little smarter than looping through each character of the question.
For example I have a PHP array, such as this one
<?php $s= array('a','b','c','d','e','f') ; ?>
And I need to loop through it in JavaScript, any ideas how do I do that?
for ( i=0 ; i < <?php echo sizeof($s) ?> ; i++) {
document.write('<?php echo $s [somehow need to get the 'i' value into here] ?>');
}
Any suggestions? Thanks!
Before your ehco/print or else your php array we make sure it's in JavaScript syntax.
<?php
$s=array('a','b','c','d','e','f');
$s_to_json=json_encode((array)$s);
?>
<script type="text/javascript">
var fromPHP=<? echo $s_to_json ?>;
for (i=0; i<fromPHP.length; i++) {
yourValue=fromPHP[i];
}
</script>
<?php
$s= array('a','b','c','d','e','f') ;
?>
<?php foreach($s as $a){ ?>
document.write('<?=$a?>');
<?php } ?>
Not tested but thats one way.
Javascript and PHP cannot be combined. They are two completely different programs that communicate only vaguely. The PHP runs on the server computer and generates the HTML. The javascript runs on the client computer in the webbrowser and acts on that HTML. If you need to move information from PHP into Javscript somehow, then you have to store it in the HTML and have the Javascript access it through that HTML. If you need to do the reverse, move information from Javascript to PHP, have the Javascript call a PHP page with a query string.
One way to place the information in your array somewhere where Javascript can get to it, would be to echo it into a hidden div. Either in a series of ided spans or just a comma separated list. Then you can pull it out of the DOM.
For example:
<div style="display: none;" id="myArray">
<?php
echo '<span id="myArray.count">'.sizeof($s).'</span>';
for ($i = 0; $i < sizeof($s); $i++) {
echo '<span id="myArray.'.$i.'">'.$s[$i].'</span>';
}
?>
</div>
Then in the Javascript you can access the array in the DOM:
var myArray = new Array();
for(i = 0; i < document.getElementById('myArray.count').innerHTML; i++) {
document.write(document.getElementById('myArray.'+i).innerHTML);
}
Disclaimer: untested code, and I don't have the time to perfect it right now. If someone else wants to comment or edit to fix any errors feel free :)
Yes.... echo out your PHP array as a JavaScript array first, and then loop over that. Don't try looping over your PHP array; you can't.
<?php $product = array('1'=>'acids','2'=>'chemical','3'=>'microbilogy'); ?>
<script>
<select name="product_id" class="form-control">
<?php foreach ($product as $key => $value)
{ echo" <option value=$value[product_id]> $value['product_name']
</option>"; } ?>
</select>
</script>
if this is helpfull please give a thumbs up
Actually you can do it, if you reverse the priority from JavaScript being the base, to PHP being the loop base.
<?php
$s= array('a','b','c','d','e','f') ;
for ( $i=0 ; $i < sizeof($s); i++)
{
?>
<script type="text/javascript">
document.write('<?php echo $s[$i]?>');
</script>
<?php
}
hey guys , i know this is a stupid question but i hanged in solving it
i wrote a block of php code to show images from mysql
echo "<table><tr> ";
while($cat = $db->sql_fetchrow($catinfo)) {
echo '
<td>
<ul id="three-col" class="press">
<li>
<div class="post-content">
<a class="zoom1" href="'.$galsettings[setImgDir].'/'.$cat[galCatLocation].'/'.$cat[galCatImg].'">
<img src="'.$galsettings[setImgDir].'/'.$cat[galCatLocation].'/'.$cat[galCatImg].'" alt="artistry (via powerbooktrance)" />
</a>
</div>';
for ($i=0; $i>2; $i++) {
echo "</tr><tr>";
}
}
echo "</li></ul></td></tr></table>";
but with this code everything goes wrong and it doesnt break after each 3 images in row
i even used
if ($i>2) {
echo "</td></tr><tr>";
}
but as u know it only breaks the tr after image number 3 not every row
im really sorry for my foolish question
Try this:
echo "<table><tr> ";
$counter = 0;
while($cat = $db->sql_fetchrow($catinfo)) {
$counter++;
echo '
<td>
<ul id="three-col" class="press">
<li>
<div class="post-content">
<a class="zoom1" href="'.$galsettings[setImgDir].'/'.$cat[galCatLocation].'/'.$cat[galCatImg].'">
<img src="'.$galsettings[setImgDir].'/'.$cat[galCatLocation].'/'.$cat[galCatImg].'" alt="artistry (via powerbooktrance)" />
</a>
</div>
';
if ( $counter == 3 ) {
echo '</tr><tr>';
$counter = 0;
}
}
echo "</li></ul></td></tr></table>";
First off, the
for ($i=0; $i>2; $i++) {
should probably be
for ($i=0; $i < 2; $i++) {
There may be additional logic issues (looking)...
Indeed the logic seems utterly flawed, for ex. there doesn't appear to be anything which detects the the third image etc...
==> I suggest you try the snippet from tambler's answer. No point in trying and fix this one... but if you must:
the for ($i=0; $i>2; $i++) { loop is unnecessary. Even when fixed to $i < 2 (or 3...) this doesn't do anything useful.
with each new image you output a <td>, this needs to be closed with a </td>
there need to be a counter (the $i) you hint at in your snippet isn't valid
the counter is to be systematically incremented with each image
a test is to be added towards the end of the loop:
If counter >= 3 (or 2, if you make it 0-based)
reset counter;
emit ""
Also the <ul> and <li> and their respective closing tags are misplaced.
Where's you're </td> tag?
All your TDs have to be inside the TRs. You have to close the UL and TD before your TR.