show/hide table in PHP code - javascript

I need to hide a table using if condition inside my following PHP code. I'm trying to use CSS for this but it seems CSS cannot read the if condition and it always runs and overrides.I want to implement it without taking table creation tags inside the if condition. Thanks
<?php
$arr=array(array(1,2,3,4,5),array(6,7,8,9));
foreach ($arr as $val)
{
echo '<table border="1" id="table1" align="center" style="float:center;">';
for($i=0;$i<sizeof($val);$i++)
{
echo '<tr><td>'.$val[$i].'</td></tr>';
if($val[$i]>5)
{?>
<style>
#table1 { visibility:hidden; }
</style>
<?php
}
else
{?>
<style>
#table1 { visibility: visible !important;}
</style>
<?php
}
}
echo'<br/>';
echo '</table>';
}
?>

<?php
$arr = array(array(1,2,3,4,5), array(6,7,8,9));
$id = 1;
?>
<html>
<head>
<style type="text/css">
.hidden {
visibility: hidden;
}
</style>
</head>
<body>
<?php
foreach($arr as $val) :
for($i = 0; $i < sizeof($val); $i++) :
if($val[$i] > 5) :
$hidden = true;
else :
$hidden = false;
endif;
endfor;
?>
<table border="1" id="table' . $id++ . '" align="center" class="<?php if($hidden) : ?>hidden<?php endif; ?>">
<?php for($i = 0; $i < sizeof($val); $i++) : ?>
<tr><td><?php echo $val[$i]; ?></td></tr>
<?php endfor; ?>
<br/>
</table>
<?php endforeach; ?>
</body>
</html>

I'm not completely sure why I am attempting to answer this. I'm not even sure which table you're trying to hide. You're also not creating a well defined relationship between the data in one table and the table that should be hidden based on the value of that data.
Your inline CSS isn't going to work as you're using the same ID multiple times. You can't do:
#table1 { visibility: hidden; }
#table1 { visibility: visible !important; }
The browser will apply the last rule to both tables. Instead use a CSS class that you generate in your loop.
.table-0 { visibility: hidden; }
.table-1 { visibility: visible !important; }
Something like this:
<?php
$arr = array( array( 1, 2, 3, 4, 5 ), array( 6, 7, 8, 9 ) );
$c = 0;
foreach ( $arr as $val ) {
$tClass = 'table-' . $c;
echo '<table border="1" class="' . $tClass . '" align="center" style="float:center;">';
for ( $i=0; $i < sizeof( $val ); $i++ ) {
echo '<tr><td>' . $val[ $i ] . '</td></tr>';
if ( $val[ $i ] > 5 ) {
?>
<style>.table-0 { visibility:hidden; }</style>
<?php
}
}
echo '<br>';
echo '</table>';
}
?>
Though this will get you what you want, there's still a lot to be desired. Mainly not injecting the CSS where you are - you're going to output a <style> for each value in the second array. Also, I'm pretty sure <style> is not an allowed child of <table>, the align and border attributes are depreciated and there is no float: center;.
I'm also wondering if you meant to use visibility: hidden; vs display: none;.
Consider doing something along these lines, could still use some improvement but better. We're not outputting more <style> than we need to.
<?php
$arr = array( array( 1, 2, 3, 4, 5 ), array( 6, 7, 8, 9 ) );
$c = 0;
$toHide = '';
foreach ( $arr as $val ) {
$tClass = 'table-' . $c;
echo '<table border="1" class="' . $tClass . '">';
for ( $i=0; $i < sizeof( $val ); $i++ ) {
echo '<tr><td>' . $val[ $i ] . '</td></tr>';
if ( $val[ $i ] > 6 ) {
$toHide = $tClass;
}
}
echo '<br>';
echo '</table>';
}
echo '<style>.' . $toHide . ' { visibility: hidden; }</style>';
?>
Some improvements could be to convert $toHide to an array and push each class into the array and then implode later if you had to handle multiple classes.

Related

How to replace a get parameter?

I have a page listing articles from a database. When user clicks a tag label they are taken to a blogsbycategory.php page. This page gets the tag parameter from the url and returns all blogs that have this tag. On the right hand side of this page I have a list of tags. How do I change the GET so when a user click a tag in the list, the url parameter will update and they can view all blogs for that chosen tag.
http://example.com/articles/blogsbycategory.php?tag=People%20management
<?php
$tag = $_GET["tag"];
$sql = "select * from blogs where tag = '$tag' " ;
$rs = mysqli_query($connect, $sql);
//get row
$fetchRow = mysqli_fetch_assoc($rs);
?>
<h3>Tags</h3>
<?php
while($row = mysqli_fetch_array($result))
{
echo '<ul style="list-style: none;">';
echo '<li>' .$row["tagName"]. '</li><hr />';
echo '</ul>';
}
?>
Something like this:
<h3>Tags</h3>
<?php
while($row = mysqli_fetch_array($result))
{
echo '<ul style="list-style: none;">';
echo '<li>' . $row["tagName"] . '</li><hr />';
echo '</ul>';
}
?>
With jQuery something like this:
<h3>Tags</h3>
<?php
while($row = mysqli_fetch_array($result))
{
echo '<ul style="list-style: none;">';
echo '<li onclick="$('#articleContainer').load(' . $row["tagName"] . ')">' . $row["tagName"] . '</li><hr />';
echo '</ul>';
}
?>
<div id="art" style="width: 600px; height: 800px; border: 1px solid grey;"></div>
List them as links
<h3>Tags</h3>
<ul>
<?php while($row = mysqli_fetch_array($result)) { ?>
<li><?php echo htmlspecialchars($row["tagName"]); ?></li>
<?php } ?>
</ul>
And please quote the data before you use them in a SQL statement.

retreive image on clicking image name through ajax in codeignitor from database

i am making gallery in codeignitor. i have retrived list of image names from database on view page. now i want to get image on clicking name through ajax. i am not that much good with code. kindly help me with fetch gallery function. any help is much appreciated. thanks in advance.
this is my code for fetchng image names:
<?php
//$i=1;
foreach($data as $row)
{
?>
<img class="img-circle img-bordered-sm" src='<?php echo base_url();?>assets/uploads/<?php echo $row->imagefiles;?>' height='30px' width='30px'>
<span class="username">
<a id="view_gallery" class="view_gallery" href="javascript:void(0);" id="<?php echo $row->id;?>"><?php echo $row->name; ?></a>
</span>
<hr>
<?php }
?>
and this is my script, i need further help with code :
<script type="text/javascript">
$( ".view_gallery" ).click(function() {
id = $(this).attr('id');
/* alert(userid);*/
$.ajax({
url : '<?php echo base_url('fetch_gallery_data') ;?>',
method : 'post',
data : {id : id},
success : function(res){
$('.disp_gallery').html(res);
}
})
});
</script>
here is my fetch gallery function in controller ,
public function fetch_gallery_data()
{
$id = $this->input->post('id');
$where = $id;
$result = $this->App_model->gallery_model('gallery','id',$where);
$data = '';
if(count($result) > 0)
{
foreach ($result as $key => $value)
{
$files = json_decode($value->imagefile);
if($value->id > 0)
{
$data .= "<div class='col-md-3' style='border:1px solid gray; margin:20px; padding:20px; ' id='".$id."''>";
$data .= "<a >";
$data .= "<img src='".base_url()."assets\uploads/seo-business-quotes-6.jpg' height='150' width='150'><br>";
$data .= "</div>";
}
else
{
foreach ($files as $key => $value)
{
$data .= "<div class='col-md-3' style='border:1px solid gray; margin:20px; padding:20px; '>";
$data .= "<img src=".base_url()."assets\uploads/".$value." height='150' width='150'><br>";
$data .= "</div>";
}
}
}
}
else
{
$data .= 'No Records.';
}
echo $data;
}
Change the slashes you mentioned in the controller then try
$data .= "<img src='".base_url()."assets/uploads/seo-business-quotes-6.jpg' height='150' width='150'><br>";
Here also change slash and single quotes
$data .= "<img src='".base_url()."assets/uploads/".$value."' height='150' width='150'><br>";

I had an issue with bootstrap's datetime picker on MozilaFirefox

Hello guys I had an issue with datetimepicker on FF. In other browsers its doing good with the code I wrote. The problem is I have three drop down options. In each one I had, time is inserted by the user using datetimepicker but unfortunately the picker works only on one of the three of them. Any idea why this is happening
PHP CODE
<?php
echo '<div class="table-rsponsive overlap">';
echo '<table class="table" >';
$workingDaysNum = array();
$counter = 0;
for ($i = 0; $i < 3; $i++) {
echo '<tr>';
for ($j = 0; $j < 7/* count($spec) */; $j++) {
if ($i == 0) {
echo '<td>' . $days[$j] . '</td>';
} else {
$counter++;
$workingDaysNum[] = 'datetimepicker' . $counter;
?>
<td>
<div class="input-group date datetimepicker" id="<?php echo $workingDaysNum[$counter - 1] ?>">
<input class="form-control " id="filter-date" size="16" type="text" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-time"></span>
</span>
</div>
</td>
<?php
}
}
echo '</tr>';
}
JQUERY CODE
$(function () {
<?php
foreach ($workingDaysNum as $workDay) {
echo '$(' . $workDay . ').datetimepicker({';
echo 'format: \'HH:mm\',';
echo 'stepping: 30';
echo '});';
}
?>
<?php
foreach ($workingDaysNum as $workDay) {
echo '$("' . $workDay . '").on("dp.change",function (e) {';
echo 'saveSpec1Modal("' . $workDay . '", $("' . $workDay . '").data(\'date\'))';
//echo 'console.log($("#'.$workDay.'").data(\'date\'))';
echo '});';
}
?>
});
Ditch the loops in the jQuery.
Instead of activating datetimepicker by ID, you have already given them all a class, so you can run it once instead of looping.
$(function () {
$('.datetimepicker').datetimepicker({
format: 'HH:mm',
stepping: 30
});
});
Same applies to the change function.

Get ID automatically in input-field after <SELECT> without button click

I have an SQL-database with many tables. Now I would like to create an input-form to be able to get data into the db without writing the entire sql-code every time. And this should work as follows:
All table names are listed in a drop-down menu. After having selected a table name, a new table with 4 columns is created automatically:
The first column of this table simply contains an increasing number.
The second column contains the field-names of the selected table.
In the third column there are empty input fields to enter the values for the database. Only in the third line (=product name) there is a drop-down menu with all product names from the main-table of the db.
The fourth column contains the data type (e.g. int or varchar)
All tables in the database have the same structure in the first 3 columns: the first column contains the table-id, the second column the foreign-key (=master_id) and the third column the product_name.
Up to this point, the script works well with the following 2 php-files (javasql.php and getuser.php):
javasql.php:
enter code here
<!DOCTYPE html>
<html>
<head>
<script>
function showUser(str) {
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
document.getElementById("txtHint").innerHTML=this.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="users" onchange="showUser(this.value)">
<option value="" class="optdrugs">please select</option>
<?php
include("files/zugriff.inc.php"); // database Access
$sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE
TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA = 'product'";
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_assoc($result)) {
echo '<option class="optdrugs" value="'. $row['TABLE_NAME'] . '">' .
$row['TABLE_NAME']. '</option>';
echo '<br>';
}
?>
</select>
</form>
<br>
<div id="txtHint"><b>Bitte Tabelle auswählen:</b>
<br>
<?php
if (isset($_POST["submit"])) {
$sent = $_POST['sent'];
$q = $_POST['tablename'];
$column_passed = unserialize($_POST['column']); // content of array
$column is passed from getuser.php
foreach ($_POST["insertvalue"] as $key => $value) {
echo $value . "<br>";
$werte[] = "'$value'";
}
$sql="INSERT INTO $q ($column_passed) VALUES (" .
implode(", ", $werte) . ")"; // data entry
mysqli_query($db, $sql);
if (mysqli_affected_rows($db) > 0) {
echo "<h3 style='color:blue'>successful</h3>";
} else {
echo "<h3 style='color:red'>not
successful</h3>";
}
}
?>
</div>
</body>
</html>
enter code here
getuser.php:
<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
padding: 5px;
}
th {text-align: left;}
</style>
</head>
<body>
<form id="formdatabase" name="formdatabase" action="javasql.php"
method="post">
<input type="hidden" name="sent" value="yes">
<?php
$q = strval($_GET['q']);
$con = mysqli_connect('localhost','root','','product');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM $q";
$result = mysqli_query($con,$sql);
$numcols = mysqli_num_fields($result); // gets number of columns in result table
$field = mysqli_fetch_fields($result); // gets the column names from the result table
$data_type_array = array(
1=>'tinyint',
2=>'smallint',
3=>'int',
4=>'float',
5=>'double',
7=>'timestamp',
8=>'bigint',
9=>'mediumint',
10=>'date',
11=>'time',
12=>'datetime',
13=>'year',
16=>'bit',
252=>'text',
253=>'varchar',
254=>'char',
246=>'decimal'
);
$data_type_array = array_flip($data_type_array);
echo "<table>";
echo "<tr>";
echo "<th>" . 'Nr' . "</th><th>" . 'Column names' . "</th>
<th>" . 'Values for db-entry' . "</th><th>" . 'Type' . "</th>";
echo "</tr>";
echo "<tr>";
$nr = 1;
for($x=0;$x<$numcols;$x++):?>
<td><?= $nr; ?></td>
<td><?= $field[$x]->name; ?></td>
<?= $column[] = $field[$x]->name; ?>
<td>
<?php
if ($field[$x]->name == 'Name') { // if-Beginn
?>
<select name="insertvalue[<?= $x; ?>]" id="insertvalue<?=
$x; ?>" size="1" onchange = "javascript:getSelectedRow()">
<?php
include("files/zugriff.inc.php");
$sql = "SELECT * FROM product_main ORDER BY Name";
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_assoc($result)) {
echo '<option class="optdrugs" value='. $row['Name'] . '>' .
$row['Name'] . '</option>';
echo '<br>';
}
?>
</select>
<?php
$name_option = "";
} else {
$name_option = "<input type='text' id='insertvalue" . $x . "'
name='insertvalue[" . $x . "]' size='50'>";
echo $name_option;
}
?>
</td>
<?php
$key = array_search($field[$x]->type, $data_type_array);
if($key !== false){
echo "<td>" . $key . "</td>";
}else{
echo "<td>" . $field[$x]->type . "</td>";
}
?>
<td><?= $field[$x]->type; ?></td>
<?= $nr = $nr + 1; ?>
</tr>
<?php endfor;
echo "</table>";
mysqli_close($con);
?>
<input type="hidden" name="tablename" value="<?= $q; ?>">
<input type="hidden" name="column" value="<?php echo htmlentities
(serialize($column)); ?>">
<input type="submit" value="Enter values" name="submit">
</form>
</body>
</html>
Since I need the master_id (= foreign key) in addition to the product-name for database entry, I would like to extend my script, so that the respective master_id is automatically sent to the input field in line 2, when a product-name is selected in line 3 ... without clicking a button. I tried to do this with javascript but it didn´t work. As far as I know, the solution would be to use AJAX but unfortunately, I am not very used to AJAX.
I would be more than happy, if someone could help me to solve this problem!

Gallery View of Images taken from DB in html

I have almost 200 images in my List.
$rings = $db->query("SELECT * FROM rings WHERE id > ". $ringId ." LIMIT 100");
I want to display them in a Gallery form with a Checkbox to each. Each Row must contains atleast 4 images with their checkboxes.
I have tried, making a for loop with adding div but that only adds vertically with on Ring at a time. How can i give it some sort of gallery look.
<div>
<?php
echo "<table><tr>";
$i =0;
foreach( $rings as $ring )
{
$i++;
echo '<td><input type="image" src="http://thevowapp.com/iphoneapp/vowstore/rings/'. $ring['imagePath'] .'" name="checked" value="' . $ring['id'].'" data-my-info="'. $ring['ringSetName'] .'" style="width:280px; height:280px;"></td>';
echo '<input type="checkbox">';
if( $i % 3 == 0 )
{
echo "</tr><tr>";
}
}
echo "</tr></table>";
?>
</div>
Your need to control the output so that it outputs horizontally until you have 4 images in that row, then start a new row.
Also, might I suggest a table structures for this.
echo "<table>
<tr>";
$i=0;
foreach( $rings as $ring ):
$i++
echo "<td><img src='{$ring['thumb']}' /></td>";
if( $i % 4 == 0 ){
echo "</tr><tr>";
}
}
echo "</tr></table>";
Consider incrementing a variable at each loop, for instance:
<?php
$loop = 0;
foreach ($items as $item) {
$loop++;
?>
<div class="col-md-3"><!-- My content --></div>
<?php
if ($loop == 4) {
$loop = 0;
?>
<div class="clearfix"></div>
<?php
}
}

Categories

Resources