I am trying to hide a "add to table" button once ONE (basically, if the table is not empty) input has been added to the table.
I tried using a IF statement but with no luck.
Since i am a begginer in this and need to do this for my homework, i would be very grateful, if you could atleast point me into the right direction.
Regards.
This is my code. I have a script that toggles a contact form trough a button click. I would like that button to dissapear if the table IS NOT empty. So 1 input = button gone. I am only addint the relavant part of the code.
EDIT: FINAL CODE.
<?php
$koncnica=".png";
$conn = mysqli_connect("localhost", "username", "pass", "DB");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT debelina, odkat, rob, vtor, id, narocilo FROM polica where id='$id'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" .
$row["debelina"] ." cm".' ✎'. "</td><td>" .
$row["odkat"].' ✎'. "</td><td>" .
$row["rob"].' <img src ="http://granital.owjej.org/wp-content/uploads/2019/03/' . $row['rob'] . $koncnica.'"height="20px"/>✎'.
"</td></tr>";}
echo "</table>";
}
$conn->close();
?>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#a').click(function() {
$('.b').toggle("slide");
});
});
</script>
<?php if ($result->num_rows == 0) { ?>
<div id="a">Dodaj</div>
<?php } ?>
<?php
$conn->close();
?>
<div class="b" style="display: none;">
<div class="container">
FORM
</div>
</div>
This does the trick :)
<?php if ($result->num_rows == 0) { ?>
<div id="a">Dodaj</div>
<?php } ?>
You can wrap the "button" HTML in some PHP which will cause it only to be rendered if the table has no items in it:
<?php if ($result->num_rows == 0) { ?>
<div id="a">Dodaj</div>
<?php } ?>
Related
PHP
I am trying to change the image during hover. For hover I used jquery, but the problem is that the first image is showing from the SQL database but the image is not changing while I already saved the second image in database
<?php
include('dbconnect.php');
$sql = "SELECT * FROM `products` JOIN `images` ON products.product_id=images.product_id";
$res = mysqli_query($conn, $sql);
echo "</h3> Total: " . mysqli_num_rows($res) . " Items Found </h3>";
?>
<br><br>
<div class="container">
<div class="row">
<?php
while ($row = mysqli_fetch_assoc($res)) {
?>
<div class="col item_col">
<?php echo $row["img_id"] . "<br>" . $row["product_id"] ."<br>". "<img src ='".$row["image_path1"]."' width=100px height=100px data-src1='".$row['image_path1']."' data-src2='".$row['image_path2']."'>"; ?>
</div>
<?php
}
?>
</div>
</div>
<?php
include('dbclose.php');
?>
jquery
<script type="text/javascript">
$(document).ready(function () {
$(".imgs").each(function(i,el){
$(this).mouseenter(function(){
$(this).attr("src",$(this).attr("data-src2"))
}).mouseleave(function(){
$(this).attr("src",$(this).attr("data-src1"))
})
})
});
</script>
Database which stores my data is this:
Now I want to fetch that data and display on my php page, but when I'm trying to fetch data in my php code I'm getting text into the following formate
UID= ????/??????????/????/?????/?????/Test upgrade/1
UID= ????/??????????/??????/??????/??????????/159/1
UID= ????/??????????/??????/??????/??????????/190/1
UID= ????/??????????/??????/??????/??????????/194/1
UID= ????/??????????/??????/???????/?????? (??.)/730/1
UID= ????/??????????/??????/???????/?????? (??.)/742/1/1
UID= ????/??????????/??????/???????/?????? (??.)/732/1
UID= ????/??????????/??????/??????/??????/98/8/1
UID= ????/??????????/??????/??????/??????/48/10/1
Referring to this question I have changed my database charset to "utf8_unicode_ci", but Still not working. I have written following code to fetch the data
datebase connection page
<?php
// Database configuration
$dbHost = "localhost";
$dbUsername = "user";
$dbPassword = "xxxxxxxxxxxxx";
$dbName = "tutorialsssxxxxx";
// Create database connection
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
// Check connection
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}
?>
and index page
<?php
include $_SERVER['DOCUMENT_ROOT']."/header.php";
?><br>
<!DOCTYPE HTML>
<html lang="hi">
<head>
<title><?php echo $_GET['dta']; ?> Tutorials Mrtutorials.net</title>
<link href='style.css' rel='stylesheet' type='text/css'>
<script src="jquery.min.js"></script>
<script type="text/javascript">
// Show loading overlay when ajax request starts
$( document ).ajaxStart(function() {
$('.loading-overlay').show();
});
// Hide loading overlay when ajax request completes
$( document ).ajaxStop(function() {
$('.loading-overlay').hide();
});
</script>
</head>
<body>
<div class="content">
<div class="dta"> <div class="list_item"><h2><?php echo $_GET['dta']; ?> Tutorials</h2></div>
<div class="post-wrapper">
<div class="loading-overlay"><div class="overlay-content">Loading.....</div></div>
<div id="posts_content">
<?php
//Include pagination class file
include('Pagination.php');
//Include database configuration file
include('dbConfig.php');
$limit = 10;
//get number of rows
$queryNum = $db->query("SELECT COUNT(*) as postNum FROM posts");
$resultNum = $queryNum->fetch_assoc();
$rowCount = $resultNum['postNum'];
//initialize pagination class
$pagConfig = array('baseURL'=>'getData.php', 'totalRows'=>$rowCount, 'perPage'=>$limit, 'contentDiv'=>'posts_content');
$pagination = new Pagination($pagConfig);
//get rows
$query = $db->query("SELECT * FROM posts Where type=$yyy ORDER BY id DESC LIMIT $limit");
if($query->num_rows > 0){ ?>
<div class="posts_list">
<?php
while($row = $query->fetch_assoc()){
$postID = $row['id'];
?>
<table width="" border="0" cellspacing="5" cellpadding="0">
<tr class="up">
<td style="font-size: 45px; padding-left:5px; padding-right:5px"><?php echo $row["id"]; ?></td>
<td valign="left" width="100%"><?php echo $row["title"]; ?> <br> <?=$value['type']?></td>
</tr>
</table>
<?php } ?>
</div>
<?php echo $pagination->createLinks(); ?>
<?php } ?>
</div>
</div></div>
</div>
</body>
</html><?php
include $_SERVER['DOCUMENT_ROOT']."/footer.php";
?>
You need to use "set_charset"
Try this: (in your index.php)
//initialize pagination class
$pagConfig = array('baseURL'=>'getData.php', 'totalRows'=>$rowCount, 'perPage'=>$limit, 'contentDiv'=>'posts_content');
$pagination = new Pagination($pagConfig);
mysqli_set_charset( $db, 'utf8');
//get rows
$query = $db->query("SELECT * FROM posts Where type=$yyy ORDER BY id DESC LIMIT $limit");
To be precise, In your case you need to add this in code where you fetching the db:
mysqli_set_charset( $db, 'utf8');
mysqli_set_charset( $db, 'utf8'); will help you set unicode on the db connection. Now to show it in the page, you will still have to set the character encoding in html.
Remember to do this. Otherwise your page will still not show you the unicode characters.
<head>
<meta charset="UTF-8">
</head>
Nothing worked for me as per reply on stackoverflow.com, to display on web page, the Hindi Text from MySql through PHP.
The following code worked for me. Please write your comments
enter code here
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM audio WHERE AudioFor= 'Aarti' ORDER BY Name";
mysqli_set_charset( $conn, 'utf8'); **// to get the hindi font/text displayed**
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) { ?>
<table cellpadding="10">
<thead>
<tr align= "centre"><h3> Other Aartis </h3></tr>
<tr>
<th>Aarti Of</th>
<th>Wordings</th>
<th>Click to Listen</th>
</tr>
<?php
while($row = mysqli_fetch_assoc($result)) {
echo "<tr><td >" . $row["Name"]."</td><td >" . $row["Wording"]. "</td><td >". ' Click To Listen '. '</td></tr>';
}
} //else {
// echo "0 results";
//}
?>
After connecting to SQL Server and Database run the following query
msql_query("SET CHARACTER SET utf8")
I'm creating a mobile app with the Ionic Framework. I got all the html pages created online. Now i want some backend code to get data from a sql server. Receiving the data is no problem with php. But when I use php pages I don't have the interface I created with Ionic.
How can I use php pages (instead of html) and still get the lay out from ionic? Example: my scorebord.php
<?php
$servername = "localhost:3306";
$username = "ssss";
$password = "dffd";
$dbname = "ddddd";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT user_username,user_city,user_highscore FROM tbl_user";
$result = $conn->query($sql);
?>
<ion-view style="" title="Scorebord">
<ion-content class="has-header" overflow-scroll="true" padding="true" style="background: url(img/hX1ml1TVGgABo3ENE6Qg_menu3.png) no-repeat center;">
<h1 style="">Scorebord</h1>
<table style="width:100%">
<?php
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
?>
<tr>
<td><?php echo $row["user_username"] ?></td>
</tr>
<?php
}
} else {
echo "0 results";
}
$conn->close();
?>
</table>
</ion-content>
</ion-view>
btw: Is it safe to just configure my database in a php file like that? Anyone a good alternative?
The Mobile App will be saved on a device which probably can't interpret PHP code, unlike a web server.
If you dont know/want javascipt then an iframe is probably your only option.
example.com/table.php
<h1 style="">Scorebord</h1>
<table style="width:100%">
<?php
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {?>
<tr> <td><?php echo $row["user_username"] ?></td></tr>
<?php
}
} else {
echo "0 results";
}
$conn->close();
?>
</table>
your app
<ion-view style="" title="Scorebord">
<ion-content class="has-header" overflow-scroll="true" padding="true" style="background: url(img/hX1ml1TVGgABo3ENE6Qg_menu3.png) no-repeat center;">
<iframe src="example.com/table.php"/>
</ion-content>
</ion-view>
But a better solution is to use JavaScript to make http requests for JSON data or full html templates(like table.php).
I would use a json backend
highscores.php
<?php
$servername = "localhost:3306";
$username = "ssss";
$password = "dffd";
$dbname = "ddddd";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT user_username,user_city,user_highscore FROM tbl_user";
$result = $conn->query($sql);
$json = mysqli_fetch_all ($result, MYSQLI_ASSOC);
echo json_encode($json);
your app
angular.module('ionicApp', [])
.controller('MainCtrl', function($scope, $http) {
$http.get('http://example.com/highscores.php').then(function(resp) {
$scope.users = resp.data;
}, function(err) {
console.error('ERR', err);
})
});
<html ng-app="ionicApp">
<body ng-controller="MainCtrl">
<ion-content>
<ion-list>
<ion-item ng-repeat="user in users">
{{user.user_username}}
</ion-item>
</ion-list>
</ion-content>
</body>
</html>
While this is a common question on stackoverflow, it seems that in every instance, the answer seems to be specific to the code belonging to the person who asked the question. After working on my own code I feel as if I am only a few rogue lines away from having this work. Essentially, what I am trying to do is filter the results of a php mysql query that has been output into a dynamic table. I want to do this by creating a select type drop down list which will sort the data from the intial php query according to different values (ie, alphabetically, or by ID). I want this all to be done client side without refreshing the page. For an example of what I'm talking about, observe the sort drop down menu on the right side of this page near the top, just below the header: http://www.walmart.com/browse/3944_3951_132982
Could someone please take a look at my code and point out what I'm still getting wrong, or at least point me in the direction of a quality tutorial. Thanks!
<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="style.css"/>
<title>Rg Distribution</title>
<script>
function sortResult(str)
{
if (str=="")
{
document.getElementById("result").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 (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("result").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","condiments.php?q"=+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="logo"><img id="mainlogo" src="rglogo.png">
<div id="header">
<div id="ulwrapleft">
<ul id="nav_left">
<li>home</li>
<li>our products</li>
</ul>
</div>
<div id="ulwrapright">
<ul id="nav_right">
<li>where to buy</li>
<li>contact</li>
</ul>
</div>
</div>
</div>
<div class="mainbody">
<form>
<select name="sortby" onchange="sortResult(this.value)">
<option value="ID" selected>ID</option>
<option value="product_name">name</option>
</select>
<button type"submit">submit</button>
</form>
<div id="result"><?php echo $dyn_table; ?></div>
</div>
</body>
</html>
PHP CODE:
<?php
$username = "";
$password = "";
$hostname = "";
$db_name = "";
$conn = mysqli_connect($hostname, $username, $password, $db_name) or die("Unable to connect");
$sql = "SELECT * FROM food_products WHERE alt_tag1 = 'condiment' ORDER BY ID";
$result = mysqli_query($conn, $sql);
$i = 0;
$dyn_table = '<table border="0" cellpadding="10">';
while($row = mysqli_fetch_array($result)){
$id = $row["ID"];
$product_name = $row["product_name"];
$image = $row["image"];
$link = $row["link"];
if ($i % 3 == 0) {
$dyn_table .= '<tr><td>' . '<img src=' . $image . '>' . '' . '<br>' . $product_name . '</td>';
} else {
$dyn_table .= '<td>' . '<img src=' . $image . '>' . '<br>' . $product_name . '</td>';
}
$i++;
}
$dyn_table .= '</tr></table>';
mysqli_close($conn);
?>
I am trying to make tabs that contain information about student, these information is stored in the database. I want the first tab to show the information of all students. and the the second tab to show the information of the students that thier grade is A and the the third tab to show the information of the students that thier grade is B.
I made this code but just the first tab shows the information of the students but the second and third tabs don't show anything.
what is wrong with the code?
<html>
<head>
<title>students table</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(function() {
$( "#tabs" ).tabs();
});
</script>
</head>
<body>
<div id="tabs">
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
<?php
$connection = mysql_connect("","","");
if (!$connection)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("", $connection);
$result = mysql_query("SELECT * FROM studentTable");
while($row = mysql_fetch_array($result))
{
?>
<div id="tabs-1">
<?php
echo "Student id: '" . $row["id"];
echo "Student name: '" . $row["name"];
echo "Student grade: '" . $row["grade"];
echo '</table>';
}
?>
</div>
<?Php
$result2 = mysql_query("SELECT * FROM studentTable WHERE grade = 'A'");
while($row = mysql_fetch_array($result2))
{
?>
<div id="tabs-2">
<?php
echo "Student id: '" . $row["id"];
echo "Student name: '" . $row["name"];
echo "Student grade: '" . $row["grade"];
echo '</table>';
}
?>
</div>
<?php
$result3 = mysql_query("SELECT * FROM studentTable WHERE grade = 'B'");
while($row = mysql_fetch_array($result3))
{
?>
<div id="tabs-3">
<?php
echo "Student id: '" . $row["id"];
echo "Student name: '" . $row["name"];
echo "Student grade: '" . $row["grade"];
echo '</table>';
}
?>
</div>
</div>
</body>
</html>
I think your issue is because you have your tabs-# <div> opening tags inside your while() loops.
$result = mysql_query("SELECT * FROM studentTable");
while($row = mysql_fetch_array($result))
{
?>
<div id="tabs-1"> //this here needs to be above/outside while($row = mysql_fetch_array($result)){
This is creating n number of <div id="tabs-1">,<div id="tabs-2">,<div id="tabs-3">, without matching closing tags </div>, so now <div id="tabs-2"> & <div id="tabs-3"> are nested in <div id="tabs-1"> and jQuery doesn't know which one to bind tabs to.
try moving them before the while() loops -
<div id="tabs-1">
<?php
$result = mysql_query("SELECT * FROM studentTable");
while($row = mysql_fetch_array($result))
{
...
}
?>
</div>
<div id="tabs-2">
<?php
$result2 = mysql_query("SELECT * FROM studentTable WHERE grade = 'A'");
while($row = mysql_fetch_array($result2))
{
...
}
?>
</div>
<div id="tabs-3">
<?php
$result3 = mysql_query("SELECT * FROM studentTable WHERE grade = 'B'");
while($row = mysql_fetch_array($result3))
{
...
}
?>
</div>
Also, you have echo '</table>'; at the end of each tab div. May want to remove those if you don't actually have a table.
In your script you are trying to use Jquery's tab method.
<script>
$(function() {
$( "#tabs" ).tabs();
});
</script>
This is looking in the HTML for
<div id='tabs'>
You're tabs in here
</div>
Assuming that your SQL statments is not returing "NULL" then I would say you're issue is that you named you're div's tab-1, tab-2, tab-3. With ID's. This means that Jquery can't find anything to turn into tab's.
Change your jquery call to something like this
<script>
$(function() {
$( ".tabsclass" ).tabs();
});
</script>
and instead of using id's use classes.
<div class='tabsclass' id='tab-1'>
You're tabs in here
</div>
<div class='tabsclass' id='tab-2'>
You're tabs in here
</div>
<div class='tabsclass' id='tab-3'>
You're tabs in here
</div>