I new to php and learning and need some advice on shoing my database info within a loop which includes html, My page is blank so am wondering why my php isnt valid:
<?php include 'incudes/connect_db.php'; ?>
<div class="topbar">
<div class="topbarblock1">Dashboard</div>
<div class="topbarblock2">Username</div>
</div>
<br><br>
<div class="middle">
<div class="middleblock1">Dashboard</div>
<div class="middleblock2">
<div class="container">
<?php
$result = mysqli_query($con,"SELECT * FROM users");
while($row = mysqli_fetch_array($result)) {
?>
<h2>
User Management
<hr />
</h2>
</div>
<div class="containermain">
<table>
<tr>
<th class="th1">User Id</th>
<th class="th2">Username</th>
<th class="th3">Email Address</th>
<th class="th4">Details</th>
</tr>
<tr>
<td class="td1">#</td>
<td class="td2"><?php echo $row['username']; ?></td>
<td class="td3"><?php $row['email']; ?></td>
<th class="td4">Edit</th>
</tr>
</table>
</div>
</div>
</div>
<?php
}
?>
I'm not looking for a straight out answer I am looking for some advice on how this should be laid out and learn best practices.
Change the line that says
<td class="td3"><?php $row['email']; ?></td>
to
<td class="td3"><?php echo $row['email']; ?></td>
PHP will try to interpret the variable as some sort of command/statement otherwise and subsequently fail.
Also rethink where you wrap the loop as I previously mentioned, or else your HTML is going to end up wonky (you'll see this after you've fixed the code.)
You should also program by using error_reporting(E_ALL); at the start of your code so you can fix simple mistakes on your own
Related
This code is use add and update product but image not showing. i think its paths error but I cant resolve it.kindly find error in this code where I mistake.Product images not showing on the product page .
i follow A series I write a same code but not working but in tutorial working fine .Tankx in advance
Product.Php
<?php
require('top.inc.php');
if(isset($_GET['type']) && $_GET['type']!=''){
$type=get_safe_value($con,$_GET['type']);
if($type=='status'){
$operation=get_safe_value($con,$_GET['operation']);
$id=get_safe_value($con,$_GET['id']);
if($operation=='active'){
$status='1';
}else{
$status='0';
}
$update_status_sql="update product set status='$status' where id='$id'";
mysqli_query($con,$update_status_sql);
}
if($type=='delete'){
$id=get_safe_value($con,$_GET['id']);
$delete_sql="delete from product where id='$id'";
mysqli_query($con,$delete_sql);
}
}
$sql="select product.*,categories.categories from product,categories where product.categories_id=categories.id order by product.id desc";
$res=mysqli_query($con,$sql);
?>
<div class="content pb-0">
<div class="orders">
<div class="row">
<div class="col-xl-12">
<div class="card">
<div class="card-body">
<h4 class="box-title">Products </h4>
<h4 class="box-link">Add Product </h4>
</div>
<div class="card-body--">
<div class="table-stats order-table ov-h">
<table class="table ">
<thead>
<tr>
<th class="serial">#</th>
<th>ID</th>
<th>Categories</th>
<th>Name</th>
<th>Image</th>
<th>MRP</th>
<th>Price</th>
<th>Qty</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
$i=1;
while($row=mysqli_fetch_assoc($res)){?>
<tr>
<td class="serial"><?php echo $i?></td>
<td><?php echo $row['id']?></td>
<td><?php echo $row['categories']?></td>
<td><?php echo $row['name']?></td>
<td><img src="<?php echo PRODUCT_IMAGE_SITE_PATH.$row['image']?>"/></td>
<td><?php echo $row['mrp']?></td>
<td><?php echo $row['price']?></td>
<td><?php echo $row['qty']?></td>
<td>
<?php
if($row['status']==1){
echo "<span class='badge badge-complete'><a href='?type=status&operation=deactive&id=".$row['id']."'>Active</a></span> ";
}else{
echo "<span class='badge badge-pending'><a href='?type=status&operation=active&id=".$row['id']."'>Deactive</a></span> ";
}
echo "<span class='badge badge-edit'><a href='manage_product.php?id=".$row['id']."'>Edit</a></span> ";
echo "<span class='badge badge-delete'><a href='?type=delete&id=".$row['id']."'>Delete</a></span>";
?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
require('footer.inc.php');
?>
When I upload pic this error show in console and pic not show on front end I try to solve couple of hours but I cant find error .i know I have little bit mistake but I cant find it.
GET http://localhost/AdminPanel/media/product/http://127.0.0.1/php/ecom/media/product/929442919_111.png 403 (Forbidden)
GetSet is right. The error is pretty quick to spot in the GET statement and the problem comes from here:
PRODUCT_IMAGE_SITE_PATH.$row['image']
I'm assuming that PRODUCT_IMAGE_SITE_PATH = http://localhost/AdminPanel/media/product/ so you'll probably need to clean the data in $row['image'] to remove the unnecessary stuff.
Important: this is not a recommended solution, but it will get you closer to something a little more logical.
Change:
PRODUCT_IMAGE_SITE_PATH.$row['image']
To:
PRODUCT_IMAGE_SITE_PATH . str_replace($row['image'], 'http://127.0.0.1/php/ecom/media/', '')
Ideally there would be a lot more data validation and whatnot going on before spitting this HTML out to a browser, but this should get you part of the way there.
I have a query that returns me a long series of questions and answers. in my html page I have div that alternate with slider effect. so far everything is fine. at the moment I try to correctly display a question from the db bumps everything. why?
<div align=Center>
<br/>
<h1><?php echo $rowfirst['Cognome']." ".$rowfirst['Nome']; ?></h1>
<br/>
<div class="nivo-slider">
<div class="navigation"></div>
<?php
while($row = mysqli_fetch_array($result))
{
?>
<div id="nivo">
<div class="element" align=Center><h3>Anamnesi pregressa</h3>
<table class="w3-table w3-bordered " style="width:400px;padding:10px;" align="center">
<tbody>
<tr>
<th scope="row"><?php echo $row['testo'];}?></th>
<td>risposta 1</td>
</tr>
</tbody>
</table>
</div>
<div class="element" align=Center><h3>Scrittura</h3>
<table class="w3-table w3-bordered" style="width:400px;padding:10px;" align="center">
<tbody>
<tr>
<th scope="row">domanda 1</th>
<td>risposta 1</td>
</tr>
</tbody>
</table>
</div>
<div class="element"><h3>Motricità</h3>
<table class="w3-table w3-bordered " style="width:400px;padding:10px;" align="center">
<tbody>
<tr>
<th scope="row">domanda 1</th>
<td>risposta 1</td>
</tr>
</tbody>
</table>
</div>
</div>
<br/>
</div>
</div>
The problem is the while. cycle. how can I solve it?
What you have will create malformed HTML.
In order to prevent malformed HTML, you should place your while () { and } where the tags matches like I have done with <tr> and </tr> shown below:
<?php
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<th scope="row"><?php echo $row['testo'];?></th>
<td>risposta 1</td>
</tr>
<?php } ?>
That will fix your malformed HTML but the while statement may or may not be in the right place and without knowing exactly what you are trying to achieve you'll have to correct it from there. As for the slider you mentioned, you probably need to supply more information if this answer doesn't solve your issue.
Not sure if this is the best way to do this.. But..
I've got a PHP file calling an API and doing a foreach loop to get all the data I want. Previously I was calling this from my html file and putting it into a table, the problem was that it was all appearing as technically the same row and that made formatting hard.
This is what I used to have:
<div class="bgimg2 w3-container">
<table class="w3-table w3-medium w3-text-white">
<tr class="w3-blue-gray">
<th> <h5> Name </h5> </th>
<th> <h5> Size </h5> </th>
<th> <h5> % </h5> </th>
</tr>
<tr>
<th> <h4> <div id=queue> </h4> </th>
<th> <h4> <div id=queueSize> </h4> </th>
<th> <h4> <div id=queuePercent> </h4> </th>
</tr>
</thead>
</table>
</div>
I want to have each item appearing as it's own row, so I'm trying to do a php foreach loop directly in the table.
This is what I've got currently for the table:
<table class="w3-table w3-medium w3-text-white">
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
<?php
require_once ('/api/sabapiQueue.php');
foreach ($apiResultDataShift as $row){
?>
<tr>
<td> <?php $row['filename'] ?> </td>
</tr>
<?php } ?>
</tbody>
</table>
It's not working and the file isn't even loading. Not sure where I've gone wrong.
Any help would be greatly appreciated.
Seems you missed a detail in not echoing out your variable. I would also suggest, and perhaps introduce you to alternative PHP syntax which is good for when you want to tightly integrate a few php blocks into html as you are doing here:
<table class="w3-table w3-medium w3-text-white">
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
<?php
require_once ('/api/sabapiQueue.php');
foreach ($apiResultDataShift as $row):
?>
<tr>
<td><?= $row['filename'] ?></td>
</tr>
<?php
endforeach;
?>
</tbody>
</table>
Alternatively, as per the echo manual page you could just change this line:
<td><?php echo $row['filename']; ?></td>
I am new in php. I want to show 5 Millions of record from my database in php in codeignter. I am using select star sql query to select table. It's working good but takes long time in loading data into datatable.
Any help would be appreciated.
Thanks in Advance.
<?php
include('layout/header.php');
include('layout/sidebar.php');
?>
<div data-widget-group="group1">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
<h2>Data Tables</h2>
<div class="panel-ctrls"></div>
</div>
<div class="panel-body no-padding table-responsive">
<form action="<?= base_url(); ?>signin/sendemailstudent" method="post">
<table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th style="padding-right:100px;">S.No</th>
<th>School</th>
<th>Date Of Admission</th>
<th>Register Serial</th>
<th>Student Name</th>
<th>Date Of Birth</th>
<th>Father Name </th>
<th>Phone </th>
<th>Email</th>
<th>Pincode</th>
<th>Class To Which Admitted</th>
<th>Class From Which Withdrawn</th>
<th>Date Of Withdrawl</th>
<th>Upload By</th>
<th>Notify<input type="checkbox" onclick="check();" id="select_all" name="select_all" /></th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
foreach ($data as $value) {
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $value['school_name']; ?></td>
<td><?php echo $value['doa']; ?></td>
<td><?php echo $value['register_serial']; ?></td>
<td><a href='<?php echo base_url('signin/editstudent').'/'.$value['id']; ?>'><?php echo $value['name']; ?></a></td>
<td><?php echo $value['dob']; ?></td>
<td><?php echo $value['father_name']; ?></td>
<td><?php echo $value['phone']; ?></td>
<td><?php echo $value['email_address']; ?></td>
</tr>
<?php
$i++;
} ?>
<div class="clearfix pt-md">
</div>
</tbody>
</table>
</form>
</div>
<div class="panel-footer"></div>
</div>
</div>
</div>
</div>
<?php include('layout/footer.php'); ?>
As someone already mentioned:
paginate
filter
'using select star', dont use the star selector if you wont use all the fields.
codeignitter is using Active Record, something that uses DataMapper could be faster, not sure if you can change that though, most probably you can (havent use codeignitter). or using pure php pdo to deal with db.
have a look on how to use generators, they're much faster for processing big sets as they dont load everything in memory at once (that's what foreach would do)
not sure which version of php you're running, but if you're working on php <5.6 then you should definitely upgrade as new versions use much less memory/cpu for dealing with db.
install sql profiler to check if your queries are optimal.
I'm using PHP in combination with an Oracle database. What I want is the following: On the first form I want to select a name out of a table in the database and when you push the button open I want the users to see a html form where the fields are filled in with the information from the person you've selected on the first screen. You can edit this information and when you push the update button the table has to be updated. I don't know who to do this whole process in PHP in combination with Oracle. Can someone please help me? This is an important part of a project I'm doing and I can't find any information anywhere!
I really hope someone can help me.
PHP & Oracle database edit/update data.
geting error Undefined variable: objResult on <<<-----here
<?
$objConnect = oci_connect("myuser", "mypassword", "TCDB");
$strSQL = "SELECT * FROM CUSTOMER";
$objParse = oci_parse($objConnect, $strSQL);
oci_execute($objParse, OCI_DEFAULT);
?>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">CustomerID </div></th>
<th width="98"> <div align="center">Name </div></th>
<th width="198"> <div align="center">Email </div></th>
<th width="97"> <div align="center">CountryCode </div></th>
<th width="59"> <div align="center">Budget </div></th>
<th width="71"> <div align="center">Used </div></th>
<th width="30"> <div align="center">Edit </div></th>
</tr>
<?
while ($objResult = oci_fetch_array($objParse, OCI_BOTH))
{
?>
<tr>
<td><div align="center"><?= $objResult["CUSTOMERID"]; ?></div></td> <<---here
<td><?= $objResult["NAME"]; ?></td> <<---here
<td><?= $objResult["EMAIL"]; ?></td> <<---here
<td><div align="center"><?= $objResult["COUNTRYCODE"]; ?></div></td>
<td align="right"><?= $objResult["BUDGET"]; ?></td> <<---here
<td align="right"><?= $objResult["USED"]; ?></td>
<td align="center">Edit</td>
</tr>
<?
}
?>
</table>
<?
oci_close($objConnect);
?>
Your while loop will probably only work for the first line. Make sure you use { } for every loop, be it one line or many.
This error could mean that short_open_tag isn't enabled in your php.ini, and due to that PHP actually sees no code in your template. Only the <?= ... ?> tags are working, while the <? ... ?> are not.
Your options:
Set short_open_tag = On in your php.ini. Or,
use full <?php ... ?> tags instead
Also, the while loop isn't complete. Try:
<?
while ($objResult = oci_fetch_array($objParse, OCI_BOTH)):
?>
// ...
<?
endwhile;
?>