Bootstrap 3 Model displaying the same row from MYSQL database - php

I am new to PHP and MYSQL, I am trying to display news data from a MYSQL db into a models. Each model should display a different [longDesc] I have the headlines and date showing correctly on the html page but when you click the button and the model pops up its only displaying the first row [longDesc] from the database across all 4 models.Instead of seperate rows, so effectively only one news story in each of the models. Can anyone point my in the right direction please.
Here is the code I am using:
<?php
$query2 = mysql_query("SELECT headline, byline, source, longDesc, newsAbbr, addedOn FROM tbl_news ORDER BY addedOn DESC LIMIT 3");
while ($temp2 = mysql_fetch_array($query2)) {
echo "<div class='row'>";
echo "<h3>".$temp2['headline']."</h3>";
echo "<p>".date("F d, Y",strtotime($temp2['addedOn']))."</p>";
echo "<a href='#' class='btn btn-sm btn-danger' data-toggle='modal' data- target='#largeModal'>Read More";
echo "</a>";
echo "</div>";
echo "<div class='modal fade' id='largeModal' tabindex='-1' role='dialog' aria-labelledby='largeModal' aria-hidden='true'>";
echo "<div class='modal-dialog modal-lg'>";
echo "<div class='modal-content'>";
echo "<div class='modal-header'>";
echo "<button type='button' class='close' data-dismiss='modal' aria-hidden='true'>×";
echo "</button>";
echo "<h5 class='modal-title' id='myModalLabel'>News</h5>";
echo "</div>";
echo "<div class='modal-body'>";
//this is the value called from the db that displays across all models (need the diff rows to be called????
echo "<p>".stripslashes($temp2['longDesc'])."</div>";
echo "</div>";
echo "<div class='modal-footer'>";
echo "<button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>";
echo "</div>";
echo "</div>";
echo "</div>";
}?>
<script src="https://code.jquery.com/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>

Related

PHP while loop problem, is not creating a new row

i try to get some data from a database and put it in a table.
the problem is the loop, the second row is not going to the new row, is continuing in same row. I checked the code and I can not see the problem.
Infact in the database i have 3 row and in the website is geting just 2 of them and booth in same row.
Can anybody help me?
<h1>view database</h1>
<div class="container">
<div class="page-header">
<h1>Shop list</h1>
</div>
<?php
$query = "SELECT shop_name, shop_phone_number, shop_email, shop_address_no, shop_address_street, shop_town, shop_county, shop_postcode, shop_address_country FROM shop";
$stmt = $conn->prepare($query);
$stmt->execute();
$num = $stmt->rowCount();
//button to add new shop
echo "<a href='index.php?page=shop' class='btn btn-primary m-b-1em'>Add new shop</a>";
if($num>0){
echo "<table class='table table-hover table-responsive table-bordered'>";
echo "<tr>";
echo "<th>Name</th>";
echo "<th>Phone</th>";
echo "<th>Email</th>";
echo "<th>Address no/name</th>";
echo "<th>Street</th>";
echo "<th>Town</th>";
echo "<th>County</th>";
echo "<th>Post Code</th>";
echo "<th>Country</th>";
echo "<th>Action</th>";
echo "</tr>";
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
echo "<tr>";
echo "<td>{$shop_name}</td>";
echo "<td>{$shop_phone_number}</td>";
echo "<td>{$shop_email}</td>";
echo "<td>{$shop_address_no}</td>";
echo "<td>{$shop_address_street}</td>";
echo "<td>{$shop_town}</td>";
echo "<td>{$shop_county}</td>";
echo "<td>{$shop_postcode}</td>";
echo "<td>{$shop_address_country}</td>";
echo "<td>";
echo "<a href='read_one.php?name={$shop_name}' class='btn btn-info m-r-1em'>Read</a>";
echo "<a href='read_one.php?name={$shop_name}' class='btn btn-primary m-r-1em' >Edit</a>";
echo "<a href='#' onclick='delete_user({$shop_name})' class='btn btn-danger>Delete</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
else{
echo "<div class='alert alert-danger'>No Records found.</div>";
}
?>
</div>
?>

multiple results with mysql and php in modal of bootstrap

I am trying to print in the modal the information of the selected user but only the first row is printed in the modal.
I am using PHP with MySQL but it is not working. I don't understand why it prints only the first row.
Please help to resolve my issue.
This is the php code:
include ("conex.php");
$query = "SELECT * FROM empleado WHERE cargo='personal'";
$datos=mysqli_query($conex,$query);
echo "<div class='tables'>";
echo "<h3 class='title1'>Listado de Empleados :</h3>";
echo "<div class='panel-body widget-shadow'>";
echo "<table class='table'>";
echo "<button class='btn btn-success add-prod'>
<span class='fa fa-plus'></span>
Agregar Empleado
</button>";
echo "<thead><tr><th>Rut</th><th>Nombre</th><th>Apellido</th><th class='action'>Accion</th></thead>";
while($fila=mysqli_fetch_assoc($datos))
{
echo "<tbody>";
echo "<tr title='informacion detallada' class='selector-empleado' data-toggle='modal' data-target='#infodetallada'>";
echo "<td>".$fila["rut"]."</td>";
echo "<td>".$fila["nombre"]."</td>";
echo "<td>".$fila["apellido"]."</td>";
echo "<td class='buttons-admin'><button title='modificar empleado' class='btn btn-primary btn-admin1'><span class='fa fa-refresh'></span></button>
<button onClick='window.location=\"eliminarempleado.php?rut=".$fila["rut"]."\";'/ title='eliminar empleado' class='btn btn-danger btn-admin2'>
<span class='fa fa-times'></span></button></td>";
echo "</tr>";
echo "</tbody>";
echo "<div class='modal fade' id='infodetallada' role='dialog'>";
echo "<div class='modal-dialog'>";
echo "<div class='modal-content'>";
echo "<div class='modal-header'>";
echo "<button type='button' class='close' data-dismiss='modal'>×</button>";
echo "<h4 class='modal-title'>Informacion detallada :</h4>";
echo "</div>";
echo "<div class='modal-body'>";
echo "<b>Rut : </b>" .$fila["rut"]."<br>";
echo "<b>Nombre : </b>" .$fila["nombre"]."<br>";
echo "<b>Apellido : </b>" .$fila["apellido"]."<br>";
echo "<b>Cargo : </b>" .$fila["cargo"]."<br>";
echo "<b>Correo : </b>" .$fila["correo"]."<br>";
echo "<b>Contraseña : </b> ************";
echo "</div>";
echo "<div class='modal-footer'>";
echo "<button title='modificar empleado' class='btn btn-primary'><span class='fa fa-refresh'></span> Modificar</button>";
echo "<button onClick='window.location=\"eliminarempleado.php?rut=".$fila["rut"]."\";'/ title='eliminar' class='btn btn-danger'><span class='fa fa-times'></span> Eliminar </button>";
echo "<button type='button' class='btn btn-default' data-dismiss='modal'>Cerrar</button>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
}
echo "</table>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "<div class='clearfix'></div>";
echo "</div>";
echo "</div>";
mysqli_close($conex);
You just have to make couple of minor changes.
Append some unique id to the data-target attribute in the clickable element, so that it is unique for each record
<tr title='informacion detallada' class='selector-empleado' data-toggle='modal' data-target='#infodetallada".$fila['rut']."'>
Append same unique id to the id attribute of the modal, so that it is same to the respective clickable element.
<div class='modal fade' id='infodetallada".$fila['rut']."' role='dialog'>
I assumed that $fila['rut'] will be unique for each of the records. If it is not so, then you can use some other unique elements.
Issue: Each model box should be connected on its own unique ID. It should have unique 'data-target' in the clickable element & 'id' in the respective modal.

Carousel String Offset in a button

I am trying to create a carousel with button "Read More" in every slide, it should be redirect to an image however it is not fetching the data instead it shows an error when im trying to put the button Illegal string offset (image2) -"image2 is the row in my sql table"
This is the carousel script.
<?php
include "db.php";
$query = "select * from carousel1 order by id desc limit 10";
$res = mysqli_query($con,$query);
$count = mysqli_num_rows($res);
while($c=mysqli_fetch_array($res)){
$titlee = $c['titlee'];
$konten = $c['konten'];
$gbr = $c['image'];
$gbr2 = $c['image2'];
if($counter==0)
{
echo"<div class='item active'>";
echo "<a href=''>";
echo "<img src='images/$gbr'>";
echo "</a>";
echo "<div class='container'>";
echo "<div class='carousel-caption left-caption style='background-color:#EE0930'>";
echo "<a href=''> <font color=#ffffffff style='font-family: Verdana,Arial,Helvetica,Georgia; font-size: 13px;'>";
echo "<h5 class='text-left'>".$titlee."</h5></font>";
echo "</a>";
echo "<a class=\"btn btn-primary btn-sm\" href=\"{$gbr2["image2"]}\">Read More</a>";
echo "</div>";
echo "</div>";
echo "</div>";
}
else
{
echo "<div class='item'>";
echo "<a href=''>";
echo "<img src='images/$gbr'>";
echo "</a>";
echo "<div class='container'>";
echo "<div class='carousel-caption left-caption style='background-color:#EE0930'>";
echo "<a href=''> <font color=#ffffffff style='font-family: Verdana,Arial,Helvetica,Georgia; font-size: 13px;'><h5 class='text-left'>".$titlee."</h5></font>
</a>";
echo "<a class=\"btn btn-primary btn-sm\" href=\"{$gbr2["image2"]}\">Read More</a>";
echo "</div>";
echo "</div>";
echo "</div>";
}
$counter++;
}
echo"</div>";
echo "<a class='left carousel-control' href='#myCarousel' data-slide='prev'>‹</a>";
echo "<a class='right carousel-control' href='#myCarousel' data-slide='next'>›</a>";
echo"</div>";
echo"<!-- End Slider Caraousel-->";
?>
The value of image2 is already being stored in $gbr2 ($gbr2 = $c['image2'];) . And then you're trying to use $gbr2["image2"] to get the value
Replace $gbr2["image2"] with $gbr2

Selecting data from sql to create a thumbnail

I'm creating a thumbnail with a title, image and caption on it. I'm trying to select data from my table to show it into my homepage. Can someone help me to create a normal thumbnail in my php that contains the detail from my sql. I tried to search and can't find how to create a thumbnail using php and not html.
$sql = "SELECT * FROM news";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>id</th>";
echo "<th>first_name</th>";
echo "<th>last_name</th>";
echo "<th>email</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['image'] . "</td>";
echo "<td>" . $row['caption'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
In short, is there a way to create a php file from this?
<div class="col-md-4">
<div class="thumbnail">
<img alt="Memory" img src="../../images/a2.jpg">
<div class="caption">
<h3><b>
Title
</b></h3>
<p>
Caption Caption Caption Caption Caption
</p>
<p align="right">
<a class="btn btn-primary" href="news2.html">Read More</a>
</p>
</div>
</div>
</div>
Do you want to replace your table structure with that template structure? You'll need to adjust some of the data to fill the hyperlink (I don't know how you want to build that).
while($row=mysqli_fetch_array($result)){
echo "<div class=\"col-md-4\">";
echo "<div class=\"thumbnail\">";
echo "<img alt=\"Memory\" src=\"../../images/{$row["image"]}\">";
echo "<div class=\"caption\">";
echo "<h3>{$row["title"]}</h3>";
echo "<p>{$row["caption"]}</p>";
echo "<p align=\"right\">";
echo "<a class=\"btn btn-primary\" href=\"news2.html\">Read More</a>";
echo "</p>";
echo "</div>";
echo "</div>";
echo "</div>";
}

Pagination in table that gets created in PHP

I have a PHP file that also has HTML/JS in it, and inside that file i use AJAX to call another PHP file which runs some queries and then echoes the results as a table back to the original php file. What i want to do is paginate the resulting table.
Searching i found this https://stackoverflow.com/questions/19605078/how-to-use-pagination-on-html-tables#= older question so i tried replicating the accepted asnwer which points to this site https://datatables.net/examples/basic_init/alt_pagination.html ,but to no result (not even an error message, which hints that i definitely lack understading of something basic).
Here is my code in the php file that gets called by AJAX:
echo '<div class="table-responsive">';
echo '<table id="resultsTable" class="table table-hover table-condensed" align="center" style="width:80%">';
echo '<thead>';
echo '<tr>';
echo '<th>Πληροφορίες</th>';
echo '<th>Εξώφυλλο</th>';
echo "<td><a href='search.php?Order=1&SearchOption=".$SearchOption."&search_db=".$SearchValue."'><button type='button' class='btn btn-primary'>Τίτλος</button></a></td>";
echo "<td><a href='search.php?Order=2&SearchOption=".$SearchOption."&search_db=".$SearchValue."'><button type='button' class='btn btn-primary'>Συγγραφέας</button></a></td>";
echo "<td><a href='search.php?Order=3&SearchOption=".$SearchOption."&search_db=".$SearchValue."'><button type='button' class='btn btn-primary'>Εκδότης</button></a></td>";
echo "<td><a href='search.php?Order=4&SearchOption=".$SearchOption."&search_db=".$SearchValue."'><button type='button' class='btn btn-primary'>Είδος</button></a></td>";
echo "<td><a href='search.php?Order=5&SearchOption=".$SearchOption."&search_db=".$SearchValue."'><button type='button' class='btn btn-primary'>Θέμα</button></a></td>";
echo "<td><a href='search.php?Order=6&SearchOption=".$SearchOption."&search_db=".$SearchValue."'><button type='button' class='btn btn-primary'>ISBN</button></a></td>";
echo '<th>Θέση</th>';
echo "<td><a href='search.php?Order=7&SearchOption=".$SearchOption."&search_db=".$SearchValue."'><button type='button' class='btn btn-primary'>Τωρινός Κάτοχος</button></a></td>";
echo "<td><a href='search.php?Order=8&SearchOption=".$SearchOption."&search_db=".$SearchValue."'><button type='button' class='btn btn-primary'>Προστέθηκε</button></a></td>";
if($LoggedIn == 1)
{
echo "<th>Αλλαγή Καταχώρησης</th>";
}
echo '</tr>';
echo '</thead>';
//body
echo '<tbody>';
for($i=0;$i<$counter;$i++)
{
echo ' <tr>';
echo "<td href='#' onclick=\"alert('".$ResultTable[$i][10]."');\" style='text-align: center;'><img src='IconImage.png' width='25' height='25' title='".$ResultTable[$i][10]."'/></td>";
echo "<td><img src='BookCovers/". $ResultTable[$i][0] ."' class='img-thubnail' alt='Δεν υπάρχει' width='50' height='50'/></td>";
echo "<td>".$ResultTable[$i][1]."</td>";
echo "<td>".$ResultTable[$i][2]."</td>";
echo "<td>".$ResultTable[$i][3]."</td>";
echo "<td>".$ResultTable[$i][4]."</td>";
echo "<td>".$ResultTable[$i][5]."</td>";
echo "<td>".$ResultTable[$i][6]."</td>";
echo "<td>".$ResultTable[$i][7]."</td>";
echo "<td>".$ResultTable[$i][8]."</td>";
echo "<td>".$ResultTable[$i][9]."</td>";
if($LoggedIn == 1)
{
echo "<td style='text-align: center;'><button type='button' class='btn btn-info'><span class='glyphicon glyphicon-wrench'></span></button>
<button type='button' class='btn btn-danger'><span class='glyphicon glyphicon-remove'></span></button></td>";
}
echo '</tr>';
}echo '</tbody>';echo '</table>';echo "</div>";
And in the original php file i included the following in the head block
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src ="jquery-1.12.3.js"></script>
<script src ="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
And just before closing the body i have
<script>$(document).ready(function() {
$('#resultsTable').DataTable( {
"pagingType": "full_numbers"
} );} );</script>
When i open the page, the results are displayed correctly, but the table is not formatted at all. Any help would be greatly appreciated.
Ok, i found the answer. The problems in the code were actually 2.
I had included 2 different versions of the jquery.
The AJAX call that was creating the table was...well..asynchronous, so the execution flow was reaching the jquery part while the table was not constructed yet. This is why i never saw an error message in the console.
I moved the jquery code from the head tag to the end of the AJAX call and it worked correctly.

Categories