Apply style to select box with forloop and php - php

I have a code like this.
If the $j or $get_book_rating value is '1' then I need to change the select box colour to red or highlight and show the value(if the value is 1). Else let it show the
normal select box with no styles applied. How can I achieve that?
<?php $get_book_rating = $row['book_rating']; ?>
<select name="<?php echo $bookName ?>" id="<?php echo $bookId ?>">
<?php for($j=0;$j<2;$j++){ ?>
<option <?php if($j == $get_book_rating) echo 'selected="selected"'; ?>><?php echo $j; ?></option>
<?php } ?>
</select>
In other words, I need to check the value of $j and also $get_book_rating and if the value is '1' i need to highlight or show some styles to the select box.
Is that possible.
Thanks,
Kimz

Hope below code may be help you- Not tested but you can use logic as i have write:
You can change condition as per your requirement
<?php $get_book_rating = $row['book_rating']; ?>
<?php
$className='';
$str='';
for($j=0;$j<2;$j++){
if($j=='1' || $get_book_rating=='1') // you need to change condition here as per your requirement
{
$className='highlight';
$str.="<option selected='selected'>". $j ."</option>";
}
else
$str.="<option>". $j ."</option>";
}
$select="<select name='".$bookName."' id='".$bookId."' class='".$className."'>";
echo $select . $str ."</select>";
?>
//Define css for class highlight in css like color,width etc

Related

PHP when entering text in a form, when submitted I need it to pull an image file from the path inputed

<td><b>Image File Name</b></td><td><input type='text' name='imgfn' id='imgfn' value='$imgfn'>
<?PHP
if(isset($_POST['imgfn']))
{
echo "
<img src='img/$imgfn' alt='$imgfn' style='width:200px;height:200px;'>
";
}
?>
so if my image is dog.jpg, in my form I would enter dog.jpg in the text box and when submitted it will pop up the dog image
I do wonder if there are many things to discuss here, but let me start with this being a more sane method of producing what it is you're asking for. There is no sense in offering a buffet if you only serve cold chicken.
Just show them the list they can pick from. Or tell them there is no food.
<?php
$dir = '/img/';
$imgs = glob($dir.'*.*');
if (empty($imgs)) :
die('this server has no images...this form will do nothing. ending output now.');
else :
?>
<td><b>Image File Name</b></td>
<td>
<select name="imgfn">
<?php
foreach ($imgs as &$img) :
?>
<option value="<?php echo str_replace('"', '\"', basename($img)); ?>"><?php echo basename($img); ?></option>
<?php
endforeach;
?>
</select>
<?PHP
if (!empty($_POST['imgfn']) && in_array($dir.$_POST['imgfn'], $imgs)) {
echo "<img src='img/$imgfn' alt='$imgfn' style='width:200px;height:200px;'>";
}
?>
</td>
<?php
endif;
?>

Dynamic link in IF/ELSE statement

I apologize for any misuse of terminology...I'm a noob...
I have a dynamically created page that contains a dynamic link. I added an IF/ELSE statement to display a different word based on the number of items in the variable $rowsphoto.
The different words display correctly, but the URL that is generated contains all of the PHP instead of generating the correct URL.
This is the original code, which works fine:
<?php if($portfolioid != 0) { ?>
<div class="extrafield">Additional works in Portfolio:</div>
This is the code I have after adding the IF/ELSE statement:
<?php if($portfolioid != 0) { ?>
<div class="extrafield">
<?php
if ($rowsphoto <= 4){
echo "Additional works in <a href='index.php?option=com_jartists&view=portfolio&aid=<?php echo $artistid;?>&pid=<?php echo $portfolioid; ?>&album=<?php echo $albumid;?>&id=<?php echo $photoidd; ?>&Itemid=105' class='portfoliocol'>Edition:</a>";
} else {
echo "Additional works in <a href='index.php?option=com_jartists&view=portfolio&aid=<?php echo $artistid;?>&pid=<?php echo $portfolioid; ?>&album=<?php echo $albumid;?>&id=<?php echo $photoidd; ?>&Itemid=105' class='portfoliocol'>Portfolio:</a>";
}
?>
</div>
I ran the code through a couple syntax checks and they all came back with no errors. What am I doing wrong? Is this even possible?
You'd be better off using it correctly like this:
<?php if($portfolioid != 0): ?>
<div class="extrafield">
<?php if($rowsphoto <= 4): ?>
Additional works in <a href='index.php?option=com_jartists&view=portfolio&aid=<?php echo $artistid; ?>&pid=<?php echo $portfolioid; ?>&album=<?php echo $albumid; ?>&id=<?php echo $photoidd; ?>&Itemid=105' class='portfoliocol'>Edition:</a>
<?php else: ?>
Additional works in <a href='index.php?option=com_jartists&view=portfolio&aid=<?php echo $artistid; ?>&pid=<?php echo $portfolioid; ?>&album=<?php echo $albumid; ?>&id=<?php echo $photoidd; ?>&Itemid=105' class='portfoliocol'>Portfolio:</a>
<?php endif; ?>
</div>
<?php endif; ?>
You're mixing html and php very badly. You should be separating them to keep your code clean and concise.
Your issue with it displaying the php instead of the correct variables is because (as #scrowler said):
You can't use PHP tags inside PHP, you just need to escape the string
bounds and use the . concatenation operator instead of trying to open
new PHP tags, e.g. echo "String here" . $varname; as opposed to echo
"String here"
While Darren's answer is correct, alternatively you can just stay inside of php
<?php
if($portfolioid != 0) {
echo '<div class="extrafield">';
if ($rowsphoto <= 4){
echo "Additional works in <a href='index.php?option=com_jartists&view=portfolio&aid=$artistid&pid=$portfolioid&album=$albumid&id=$photoidd&Itemid=105' class='portfoliocol'>Edition:</a>";
} else {
echo "Additional works in <a href='index.php?option=com_jartists&view=portfolio&aid=$artistid&pid=$portfolioid&album=$albumid&id=$photoidd&Itemid=105' class='portfoliocol'>Portfolio:</a>";
}
echo '</div>';
}
?>

how to getElementByID() when the id is a php variable?

For example:
<?php foreach($something as $anotherthing){ ?>
<span id="<?php echo $product_id; ?>"><?php echo $price; ?></span>
<?php if($option == 'select') { ?>
<select name="joe" id="<?php echo $select_id; ?>" > ......
I have no idea how to get the id's into javascript.
HTML is text. JavaScript is text. So - the same way.
getElementById('<?php echo $product_id; ?>');
The same way as in HTML :
document.getElementByID('<?php echo $id?>');
Suggestion, either use $_SESSION or put the variable into a hidden input field that always has the same name, then you can get the value from there and use it as the ID :)
Given you use a syntax similar to product-N or product[N] if you only have a want all products on the page:
document.querySelectorAll('[id^=product]');
if you want just one of those you can use only the first match add [0] just before the ;

change an image via php dropdown

What I am trying to do is change the image when an item is selected from the drop down. This is part of a form so I cant have the value change. However the option value is the row id, that row would also contain the target for the image. But because the target 'file' is called outside the loop it isn't firing.
I read I have to call it within the loop first but can't get it to work. Could you look at the code below and throw me a hint?
Thanks
<?php
include ("conned-db.php");
$result = mysql_query("SELECT * FROM gallery")
or die(mysql_error());
echo "<select id='gallery_id' name='gallery_id' style='width:200px;' >";
while($row = mysql_fetch_array( $result ))
{
echo '<option value=' . $row['id'] . '>';
echo $row['gallery_name'];
echo '</option/>';
}
echo "</select>";
echo "</td>";
echo "<td colspan='2' rowspan='2'>";
echo '<img src=' .$row['file']. '/></td>';
?>
Try this, I think this is what you are looking for
If you want to do something like this you must use Ajax. Here you go for the link that helps you to understand about Ajax.
http://www.w3schools.com/php/php_ajax_database.asp
Note:
If you wanted it to be only PHP without Javascript, you would have to sacrifice the 'must not refresh' constraint, as the only way to submit the form is by pressing the button, and submitting the content.
This should work too. If the file locations of the images are available at the time you load the page using ajax is not a must. You have to use ajax if you need to query the server again to retrieve the required file location. The following code assumes that you have the location of the images for each item of the dropdown list at the time you load the page.
<select id='gallery_id' name='gallery_id' style='width:200px;'
onchange='document.getElementById("image").src=this.options[this.selectedIndex].title' >
<?php
while($row = mysql_fetch_array( $result ))
{
?>
<option value='<?php echo $row["id"]; ?>' title='<?php echo $row["file"]; ?>'>
<?php echo $row["gallery_name"]; ?>
</option>
<?php
}
?>
<img id="image" />
Here is a short example which implements java scrip and php where i update src of an image based on the id from the select you might want to change with specific src based on that id
<?php
include ("conned-db.php");
$item = $_GET["imageid"];
if ($item == "")
{
$item = 1;
}
$result = mysql_query("SELECT * FROM gallery")
or die(mysql_error());
?>
<select id='gallery_id' name='gallery_id' onChange="window.location='file.php?imageid='+this.value" style='width:200px;' >
<?
while($row = mysql_fetch_array( $result ))
{
echo '<option value=' . $row['id'] . '>';
echo $row['gallery_name'];
echo '</option/>';
}
?>
</select>
</td>
<?
echo "<td colspan='2' rowspan='2'>";
?>
<img src=' <?=$item?> '/></td>

using php + javascript to change image from select

Basically, I am doing this: I have already read the database and obtained all the items, each includes a 'name' and a 'link'. On the page, I have an image, a select menu, and a button; each select option displays an item name (but no link information). I want to implement this: first select an option (nothing happen), and then click the button to change the image src to the selected item's link (in PHP variable). But I have trouble to pass the link to the javascript function, because it involves PHP variable. The code is like this:
<script type="text/javascript">
function selectItem(i) {
var name, link;
/*
...
*/
document.getElementById("name").innerHTML = name;
document.getElementById("pic").src = link;
}
</script>
...
<body>
<img id="pic" src="default.png">
<div id="name"></div>
<div onclick="location.href='javascript:selectItem(document.
getElementsByName("menu")[0].value)';"></div>
<select name="menu">
<option value="">select one</option>
<?php
$i = 0;
foreach ($items as $item):
echo ("<option value=\"" . $i . "\">" . $item['name'] . "</option>");
$i++;
endforeach;
?>
</select>
</body>
Thanks for helping!
Is the foreach loop actually looping? Try:
<?php
$i = 0;
foreach ($items as $item) { ?>
<option value="<? echo $i ?>"><? echo $item['name'] ?></option>
<? $i++; }
?>
I know this is late, but my solution is: to record name and link together (separated by " | ") in the attribute "value" of each option. Then there is no need to pass PHP variable to the JavaScript.

Categories