Show Data database only with specific ID - php

I have this code to show all data in my database:
<?php foreach($alldatas as $data): ?>
<div class="col-md-4">
<div class="card">
<div class="content">
<h3><?php echo $data['data_name'];?></h3>
<div class="footer text-center">
<?php echo $data['data_version'];?>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
That code works perfect to show all my data with different data_name and data_version. But what I want is to show the data only with specific ID.
So I want to make it work with bootstrap navtabs:
<div class="col-md-12">
<ul class="nav nav-pills nav-tabs nav-justified" role="tablist">
<li role="presentation" class="active">Pro</li>
<li role="presentation">Middle</li>
<li role="presentation">Low</li>
</ul>
</div>
<?php foreach($alldatas as $data): ?>
<div class="tab-content">
<div role="tabpanel" class="tab-pane fade in active" id="pro">
<?php if($data['id'] == "2" | $data['id'] == "3" | $data['id'] == "4"){?>
<div class="col-md-4">
<div class="card">
<div class="content">
<h3><?php echo $data['data_name'];?></h3>
<div class="footer text-center">
<?php echo $data['data_version'];?>
</div>
</div>
</div>
</div>
<?php } ?>
</div>
<div role="tabpanel" class="tab-pane fade" id="middle">
<?php if($data['id'] == "5" | $data['id'] == "6" | $data['id'] == "7"){?>
<div class="col-md-4">
<div class="card">
<div class="content">
<h3><?php echo $data['data_name'];?></h3>
<div class="footer text-center">
<?php echo $data['data_version'];?>
</div>
</div>
</div>
</div>
<?php } ?>
</div>
</div>
<?php endforeach; ?>
But it just only show data with ID 1,2,3 when I try to click other tabs. How to fix this?

You are having foreach loop on "tab-content" which is wrong, actually, it will create so many "tab-content"s and that's not required. You MUST loop for content not "tab-content".
So, first fix would be:
<div class="tab-content">
<div role="tabpanel" class="tab-pane fade in active" id="pro">
<?php foreach($alldatas as $data): ?>
<?php if($data['id'] == "2" | $data['id'] == "3" | $data['id'] == "4"){?>
<div class="col-md-4">
<div class="card">
<div class="content">
<h3><?php echo $data['data_name'];?></h3>
<div class="footer text-center">
<?php echo $data['data_version'];?>
</div>
</div>
</div>
</div>
<?php } ?>
<?php endforeach; ?>
</div>
<div role="tabpanel" class="tab-pane fade" id="middle">
<?php foreach($alldatas as $data): ?>
<?php if($data['id'] == "5" | $data['id'] == "6" | $data['id'] == "7"){?>
<div class="col-md-4">
<div class="card">
<div class="content">
<h3><?php echo $data['data_name'];?></h3>
<div class="footer text-center">
<?php echo $data['data_version'];?>
</div>
</div>
</div>
</div>
<?php } ?>
<?php endforeach; ?>
</div>
</div>
It will resolve your issue, however, if it still persists, you can implement a jquery function for custom toggling of "active" class. (Still, first correct the position of "foreach" as shown above).

Related

I have an error in my code that does not allow me to show the results and/or show an error message in php

I have a little problem that might be syntax (since I'm not that good at php). Basically, I'm de-wrapping a code where there will be records where there will be a date (in this case, a foundation date, for example; 20-08-2027). So I created an "if". If there are records with the date, for example, 2027, the records appear. If there is not, then an error message will be displayed saying that there is still no record made with that date. I've tried a few things, but nothing worked. At this point, an error appears. Below will be the error. Please try to help me. It's no use saying more technical things, because I'm not that good at php. Thank you.
Error: "Parse error: syntax error, unexpected 'else' (T_ELSE) in /home/host/public_html/template/year/2027.php on line 300"
2027.php
<section class="content products">
<div class="container">
<h1 class="hidden">Eventos</h1>
<div class="row">
<div class="col-sm-10">
<div class="row grid" id="products">
<div class="releated-products">
<div class="row grid" id="products">
<?php
$sqli=sprintf("SELECT * FROM eventos WHERE YEAR(data) = 2027");
$resu=mysqli_query($con,$sqli);
mysqli_set_charset($con, 'UTF8');
if (mysqli_num_rows($resu)>0) {
while($regi=mysqli_fetch_array($resu)){
$sqli_consulta=sprintf("select * from eventos where id=%d;",$regi['id']);
$resu_consulta=mysqli_query($con,$sqli_consulta);
$regi_consulta=mysqli_fetch_array($resu_consulta);
$linkk='../eventoindividual.php?id='.$regi_consulta['id'];
?>
<div class="col-sm-3 col-xs-6">
<article class="product-item">
<div class="row">
<div class="col-sm-3">
<div class="product-overlay">
<div class="product-mask"></div>
<img src="<?php echo '../admin/documentos/'.$regi['nome_doc']; ?>" style="width:100%">
</div>
</div>
<div class="col-sm-9">
<div class="product-body">
<h3><?php echo $regi['nome']; ?></h3>
<br>
<span class="price">
<ins><span class="amount"><?php echo $regi['preco']; ?></span></ins>
</span>
<div class="buttons buttons-simple">
<i class="fa fa-shopping-cart"></i>Comprar bilhetes
</div>
</div>
</div>
</div>
</article>
</div>
<?php
} else{
?>
<p>Nada!</p>
<?php
}
}
?>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
The reason you are getting the error because you have while loop in your if block that you are not closing.
Check the code below for this fixed version.
<?php
$sqli=sprintf("SELECT * FROM eventos WHERE YEAR(data) = 2027");
$resu=mysqli_query($con,$sqli);
mysqli_set_charset($con, 'UTF8');
if (mysqli_num_rows($resu)>0):
while($regi=mysqli_fetch_array($resu)):
$sqli_consulta=sprintf("select * from eventos where id=%d;",$regi['id']);
$resu_consulta=mysqli_query($con,$sqli_consulta);
$regi_consulta=mysqli_fetch_array($resu_consulta);
$linkk='../eventoindividual.php?id='.$regi_consulta['id'];
?>
<div class="col-sm-3 col-xs-6">
<article class="product-item">
<div class="row">
<div class="col-sm-3">
<div class="product-overlay">
<div class="product-mask"></div>
<img src="<?php echo '../admin/documentos/'.$regi['nome_doc']; ?>" style="width:100%">
</div>
</div>
<div class="col-sm-9">
<div class="product-body">
<h3><?php echo $regi['nome']; ?></h3>
<br>
<span class="price">
<ins><span class="amount"><?php echo $regi['preco']; ?></span></ins>
</span>
<div class="buttons buttons-simple">
<i class="fa fa-shopping-cart"></i>Comprar bilhetes
</div>
</div>
</div>
</div>
</article>
</div>
<?php endwhile; else: ?>
<p>Nada!</p>
<?php
endif;
?>
Move the last } bracket above }else{
<section class="content products">
<div class="container">
<h1 class="hidden">Eventos</h1>
<div class="row">
<div class="col-sm-10">
<div class="row grid" id="products">
<div class="releated-products">
<div class="row grid" id="products">
<?php
$sqli=sprintf("SELECT * FROM eventos WHERE YEAR(data) = 2027");
$resu=mysqli_query($con,$sqli);
mysqli_set_charset($con, 'UTF8');
if (mysqli_num_rows($resu)>0) {
while($regi=mysqli_fetch_array($resu)){
$sqli_consulta=sprintf("select * from eventos where id=%d;",$regi['id']);
$resu_consulta=mysqli_query($con,$sqli_consulta);
$regi_consulta=mysqli_fetch_array($resu_consulta);
$linkk='../eventoindividual.php?id='.$regi_consulta['id'];
?>
<div class="col-sm-3 col-xs-6">
<article class="product-item">
<div class="row">
<div class="col-sm-3">
<div class="product-overlay">
<div class="product-mask"></div>
<img src="<?php echo '../admin/documentos/'.$regi['nome_doc']; ?>" style="width:100%">
</div>
</div>
<div class="col-sm-9">
<div class="product-body">
<h3><?php echo $regi['nome']; ?></h3>
<br>
<span class="price">
<ins><span class="amount"><?php echo $regi['preco']; ?></span></ins>
</span>
<div class="buttons buttons-simple">
<i class="fa fa-shopping-cart"></i>Comprar bilhetes
</div>
</div>
</div>
</div>
</article>
</div>
<?php
} // while()
} else{
?>
<p>Nada!</p>
<?php
}
// } //wrong bracket
?>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
For your own sanity it is a good idea leave a comment after closing bracket of a long code, so you know which block it belongs to.
Compiler can't be wrong, if it says there's syntax error then there is. The bracket before the else is closing the while loop - hence else cannot be used there, close the if condition. Here's the corrected code:
<section class="content products">
<div class="container">
<h1 class="hidden">Eventos</h1>
<div class="row">
<div class="col-sm-10">
<div class="row grid" id="products">
<div class="releated-products">
<div class="row grid" id="products">
<?php
$sqli=sprintf("SELECT * FROM eventos WHERE YEAR(data) = 2027");
$resu=mysqli_query($con,$sqli);
mysqli_set_charset($con, 'UTF8');
if (mysqli_num_rows($resu)>0) {
while($regi=mysqli_fetch_array($resu)) {
$sqli_consulta=sprintf("select * from eventos where id=%d;",$regi['id']);
$resu_consulta=mysqli_query($con,$sqli_consulta);
$regi_consulta=mysqli_fetch_array($resu_consulta);
$linkk='../eventoindividual.php?id='.$regi_consulta['id'];
/* 2 brackets { */
?>
<div class="col-sm-3 col-xs-6">
<article class="product-item">
<div class="row">
<div class="col-sm-3">
<div class="product-overlay">
<div class="product-mask"></div>
<img src="<?php echo '../admin/documentos/'.$regi['nome_doc']; ?>" style="width:100%">
</div>
</div>
<div class="col-sm-9">
<div class="product-body">
<h3><?php echo $regi['nome']; ?></h3>
<br>
<span class="price">
<ins><span class="amount"><?php echo $regi['preco']; ?></span></ins>
</span>
<div class="buttons buttons-simple">
<i class="fa fa-shopping-cart"></i>Comprar bilhetes
</div>
</div>
</div>
</div>
</article>
</div>
<?php
} // while loop
} // if condition
else {
?>
<p>Nada!</p>
<?php
}
?>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
hope this helps. 👍

all db records get updated instead of only 1 CodeIgniter 4

I am working on a school project.
I have a validateUser function in my Users.php controller:
public function validateUser()
{
$data = [];
UserModel::validateUser($this->request->getPost('id_user')); // validate the user
if (session()->get('success'))
{
return redirect()->to('admin_panel');
}
else
{
echo view('templates/header', $data);
echo view('admin_panel2',$data);
echo view('templates/footer');
}
}}
This is my validateUser function in my UserModel.php
public static function validateUser($id_user)
{
$model = new UserModel();
$data = ['id_role' => '2'];
$model->update($id_user,$data);
session()->setFlashdata('success', 'Successfuly Validated');
}
My view
<div class="container">
<div class="row">
<div class="col-12">
<div class="container">
<p></p>
<div class="panel-group">
<div class="panel panel-default">
<div class="panel-heading">
<h6 class="panel-title">
<a data-toggle="collapse" href="#collapse1">
<?php
echo "<h3>Users To Validate</h3>";
?>
</a>
<?php
?>
</h6>
</div>
<div id="collapse1" class="panel-collapse collapse">
<div class="panel-body">
<h4></h4>
<ol>
<div class="container">
<div class="row">
<div class="col-12 col-sm8- offset-sm-2 col-md-6 offset-md-3 mt-5 pt-3 pb-3 bg-white form-wrapper">
<div class="container">
<?php if (session()->get('success')) : ?>
<div class="alert alert-success" role="alert">
<?= session()->get('success') ?>
</div>
<?php endif; ?>
<?php foreach ($usersToValidate as $row)
echo '<li class="nav-item">' . $row->firstname . ' ' . $row->lastname . '<a class="nav-link active" href="validate_user?id_user='.$row->id_user.'">Validate</a></li>';
?>
<ul class="nav">
</ul>
</div>
</div>
</div>
</div>
</ol>
</div>
<div class="panel-footer">
</div>
</div>
</div>
</div>
</div>
<div class="container">
<hr>
<?php if (isset($permissions)) : ?>
<div class="col-12">
<div class="alert alert-danger" role="alert">
</div>
</div>
<?php else : ?>
<?php endif; ?>
<?php if (isset($validation)) : ?>
<div class="col-12">
<div class="alert alert-danger" role="alert">
<?= $validation->listErrors() ?>
</div>
</div>
<?php else : ?>
<?php endif; ?>
</div>
</div>
</div>
The problem is that in my 'users' table all 'id_role' records get changed to '2'. I only wish to update a single record in the db.
Any idea what I'm doing wrong ?
I found the issue. I replaced $this->request->getPost('id_user') with getGet('id_user') in my controller function
public function validateUser()
{
$data = [];
UserModel::validateUser($this->request->getGet('id_user')); // validate the user
if (session()->get('success'))
{
return redirect()->to('admin_panel');
}
else
{
echo view('templates/header', $data);
echo view('admin_panel2',$data);
echo view('templates/footer');
}
}

How to Active Loop Tab PHP

I don't know what to do loop and active tab on php.Active only one tab at a time.
My tab
<div class="container content">
<div class="row tab-v3">
<div class="col-sm-3">
<?php do { ?>
<ul class="nav nav-pills nav-stacked">
<li><i></i><?php echo $row_menulist['mlname']; ?></li>
</ul>
<?php } while ($row_menulist = mysql_fetch_assoc($menulist)); ?>
</div>
<div class="col-sm-9">
<div class="tab-content">
<div class="tab-pane fade in active" id="<?php echo $row_menulist['mlname']; ?>">
<?php do { ?>
<h4><?php echo $row_info['mlname']; ?></h4>
<p><?php echo $row_info['detail']; ?></p>
<?php } while ($row_info = mysql_fetch_assoc($info)); ?>
</div>
</div>
</div>
</div>
</div>
remove active class,i assume that change in loop not affect in design.
<div class="container content">
<div class="row tab-v3">
<div class="col-sm-3">
<?php do { ?>
<ul class="nav nav-pills nav-stacked">
<li><i></i><?php echo $row_menulist['mlname']; ?></li>
</ul>
<?php } while ($row_menulist = mysql_fetch_assoc($menulist)); ?>
</div>
<div class="col-sm-9">
<div class="tab-content">
<?php $count = 1; do { ?>
<div class="tab-pane fade in <?php if($count == '1') { echo 'active'; } ?>" id="<?php echo $row_menulist['mlname']; ?>">
<h4><?php echo $row_info['mlname']; ?></h4>
<p><?php echo $row_info['detail']; ?></p>
</div>
<?php $count++; } while ($row_info = mysql_fetch_assoc($info)); ?>
</div>
</div>
</div>
</div>
It's working!
I even use the same logic into a CMS PHP Based (SPIP).
<div class="row">
<div class="col-xs-12 col-sm-3">
<div class="w-100 srvc-tab">
<ul class="nav nav-tab">
<?php $count = 1; ?>
<BOUCLE_getServiceTitle (ARTICLES){id_rubrique=#ID_RUBRIQUE}{par id_article}{tous}>
<li class="<?php if($count == '1') { echo 'active'; } ?>">
<a href="#service-#COMPTEUR_BOUCLE" data-toggle="tab">
[(#TITRE)]
</a>
</li>
<?php $count++; ?>
</BOUCLE_getServiceTitle>
</ul>
</div>
</div>
<div class="col-xs-12 col-sm-9">
<div class="w-100 tab-content">
<?php $count = 1; ?>
<BOUCLE_getServiceContent (ARTICLES){id_rubrique=#ID_RUBRIQUE}{tous}>
<div id="service-#COMPTEUR_BOUCLE" class="col-xs-12 tab-pane fade in <?php if($count == '1') { echo 'active'; } ?>">
<div class="w-100 mb-15 pb-15">
<h3 class="mb-0 text-capitalize fw-600">
[(#TITRE)]
<span class="d-inline-block stroke"></span>
</h3>
</div>
<div class="w-100">
[(#TEXTE)]
</div>
</div>
<?php $count++; ?>
</BOUCLE_getServiceContent>
</div>
</div>
<div class="clearfix"></div>
</div>

Add a Row After 6 Records Automatically

Here Is The Code
<div class="row">
<div class="col-md-9 column">
<div class="services-listing">
<?php if(!empty($data)):
foreach ($data as $rows ) : ?>
<div class="service">
<div class="service-img"><span><img src="dashboard/uploads/<?php echo $rows['img'];?>" alt="" /></div>
<div class="service-detail">
<h3><?php echo $rows['name'];?></h3>
</div>
</div>
<!-- Service -->
<?php endforeach; else : echo "No Record Found Against This Services"; endif;?>
</div>
<!-- Service Listing -->
</div>
i want to start design from <div class="row"> not from <div class="service"> now it repeating this div i want new <div class="row"> genrated after every 6 records
<div class="row">
<div class="col-md-9 column">
</div>
</div>
Looking for something like this, let me know if it works for you
<?php
$start = 6;
$end = 1;
if(!empty($data)){
foreach ($data as $rows ) {
if ($start % 6 == 0) { ?>
<div class="row">
<div class="col-md-9 column">
<?php } ?>
<div class="services-listing">
<div class="service">
<div class="service-img"><span><img src="dashboard/uploads/<?php echo $rows['img'];?>" alt="" /></div>
<div class="service-detail">
<h3><?php echo $rows['name'];?></h3>
</div>
</div>
<!-- Service -->
</div>
<?php if ($end % 6 == 0) { ?>
</div>
</div>
<?php } $start++; $end++;
}
}
else{ echo "No Record Found Against This Services"; }
?>
you need to wrap div row with foreach first and then:
<?php $i = 0; ?>
<?php if(!empty($data)):
foreach ($data as $rows ) : ?>
<?php $i++; ?>
<div class="row">
<div class="col-md-9 column">
<div class="services-listing">
<div class="service">
<div class="service-img"><span><img src="dashboard/uploads/<?php echo $rows['img'];?>" alt="" /></div>
<div class="service-detail">
<h3><?php echo $rows['name'];?></h3>
</div>
<!-- Service -->
</div>
<!-- Service Listing -->
</div>
<!-- col-md-9 column -->
</div>
<!-- Row -->
</div>
<?php if($i%6==0):?>
<div class="row">
<div class="col-md-9 column">
</div>
</div>
<?php endif; ?>
<?php endforeach; else : echo "No Record Found Against This Services"; endif;?>
just please re-check all divs structure to be sure whether all of them are closed

How to hide certain id/class in html if variable of an array is empty

Im new to php.
Hide tab If that array varible is empty.
<ul id="myTab" class="nav nav-tabs nav-justified">
<li class="active"><i class="fa fa-tree"></i> English
</li>
</li>
<li class=""><i class="fa fa-car"></i> Chords
</li>
</ul>
<div id="myTabContent" class="tab-content">
<div class="tab-pane fade active in" id="eng">
<div class="lyrics-body-txt">
<div class="english"> `<?php echo $song["eng"]; ?>`
</div></div>
</div>
<div class="tab-pane fade" id="cho">
<div class="lyrics-body-txt">
<div class="chords"> `<?php echo $song["chords"]; ?>`
</div></div>
</div></div></div>'
"english" => "bla bla bla bla bla",
"chords" => "", ---- This is empty
So i want to hide that chords tab when ever the chords variable is empty
<div id="myTabContent" class="tab-content">
<div class="tab-pane fade active in" id="eng">
<div class="lyrics-body-txt">
<div class="english"> <?php echo $song["eng"]; ?>
</div></div>
</div>
<?php
if($song["chords"] != '')
{
?>
<div class="tab-pane fade" id="cho">
<div class="lyrics-body-txt">
<div class="chords"> <?php echo $song["chords"]; ?></div>
</div>
</div>
<?php
}
?>
</div>
If you want to hide the div just add the hidden class
<div class="chords <?php if(empty($song["chords"]) echo 'hidden'; ?>">
If you don't want it:
<?php
if(!empty($song["chords"])) {
echo '
<div class="tab-pane fade" id="cho">
<div class="lyrics-body-txt">
<div class="chords"> '.$song["chords"].'
</div></div>
</div>
';
}
?>

Categories