fill jquery tabs header with information from database - php

I have the structure of a Tab component, but I will like that the head of each tab is the name of a person that is in the database stored.
For example, normally this is a tabs structure
<div id="tabs">
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
<div class="panels">
<div id="tab-1">
</div>
<div id="tab-2">
</div>
<div id="tab-3">
</div>
</div>
</div>
But instead of Tab 1,2 and 3 as head text for each tab, I will like to have Marc, Josef, Luis and these are name from the DB. This also includes that everytime that a person is added I will have a new tab. Has anybody a idea how I can do this?
I tryed to call the result of the PHP query in the text of the head, but was not working
**********EDITED WITH CODE******************
<?php
$connection = mysql_connect(/*DAta connectionns*/);
if (!$connection)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("", $connection);
$result = mysql_query("SELECT * FROM People");
while($row = mysql_fetch_array($result))
{
$r = $row["name"];
<div id="tabs">
<ul>
<li><?php $r ?></li>
</ul>
<div class="panels">
</div>
</div>
}
?>
I know that there is something wrong, but I can not see

Assume you have a JavaScript Object (from your ajax?) you can do:
var mytabs = [{
name: "Marc"
}, {
name: "Josef"
}, {
name: "Luis"
}];
$('#tabs').tabs();
$.each(mytabs, function(indexInArray, myobj) {
$('#tabs').find('li> a').eq(indexInArray).text(myobj.name);
});
Here is a fiddle you can play with: http://jsfiddle.net/MarkSchultheiss/p4mzy3hb/

Related

How to insert echo into div

I'm new here, so do not be angry if I ask something that is already answered.
I connected sql database:
connect.php
<?php
$connect = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("mobilni", $connect) or die(mysql_error());
?>
displaydata.php
<?php
include "connection.php";
$sql= "SELECT * FROM imena WHERE Okrug='Beogradski'";
$query=mysql_query($sql) or die (mysql_error());
?>
<div class="beyondheader"></div>
<div class="header">
<div id='cssmenu'>
<ul>
<li class='active'><a href='index.html'>Početna</a></li>
<li><a href='proizvodjaci.html'>Proizvodjači</a></li>
<li><a href='oglasi.html'>Oglasi</a></li>
<li><a href='about.html'>O nama</a></li>
</ul>
</div>
</div>
<div class="middle">
<div class="leftmiddle">ss
</div>
<div class="rightmiddle">
<?php
while ($row = mysql_fetch_array($query)){
?>
<div class="divmobilni">
Ime:<div class="mobilniime"><?php echo $row['Ime'];?></div>
Okrug:<?php echo $row['Okrug'];?>
</div>
<?php } ?>
</div>
</div>
And everything is working fine. Now I want to put every result in the other <div> automatically?
How to do that?
Sorry if I wasn't clear enough. I want to sort results to be like this, in one <div> goes: Ime: Okrug: [that is data for one person]
Now, I want to make that all data from my SQL table display on this page, but every Person separately from this <div>.
To be something like this, with data from table: (This is just example drawn in Paint)
I fixed this with adding only break to the end of PHP. Thanks in any case!
Exactly the same as you would with php open and close <?php ?>
Use this inside the divs you would like or just use echo with the div inside

how to retrieve data from mysql database and populate in list view in jquery mobile

please someone help me to find the solution for how to retrieve data from mysql database and populate in list view in jquery mobile.
am having the php code as follows
<? php
include('libraries/config.php');
$result = mysql_query("SELECT * from category") or die(mysql_error());
while ($obj = mysql_fetch_object($result)) {
$arr[] = $obj;
}
echo json_encode($data);
echo '{"Deals":'.json_encode($arr).'}';
?>
here am getting the data from mysql in json format but i was not known how to populate this in listview, my html page is as follows
<div id="content-area" style="height:auto;">
<br/>
<ul data-role="listview">
<li>
<a href="comfort_list.html">
<div class="content-home-tab1">
<div class="img-content">
<img id="ic_home" src="images/next_btn.png" style="margin-top:37px; margin-left:448px;" width="22" height="28" />
</div>
<div class="content-home-p">
<b>Comfort</b>
</div>
</div>
</a>
</li>
<li>
<a href="#">
<div class="content-home-tab1">
<div class="img-content">
<img id="ic_home" src="images/next_btn.png" style="margin-top:37px; margin-left:448px;" width="22" height="28" />
</div>
<div class="content-home-p">
<b>Handling Your Lenses</b>
</div>
</div>
</a>
</li>
</ul>
</div>
Here in my code am giving static data to list that is comfort and handling your lens, am having this data in db and i need to get that and to be placed here.
in the page i am posting in a form like this
<form method="post" action="help.html">
<ul data-role="listview" >
<?php while ($row = $stmt->fetch()){
?>
<li>
<a href="help1.php?id=<?php echo $row['categoryID']; ?>">
<div class="content-home-tab1">
<div class="img-content">
<img id="ic_home" src="images/next_btn.png" style="margin-top:37px; margin-left:448px;" width="22" height="28" />
</div>
<div class="content-home-p">
<b><?php echo $row['title'];?>
</b>
</div></div></a>
</li>
<?php }?>
</ul>
</form>
and in another page am trying to get the id as follows
<?php
include('libraries/config.php');
$getID = $_GET['id'];
echo $getID;
$stmt = $db->prepare("SELECT * from category where categoryID ='$_GET['id']'");
$stmt->execute();
?>
but when going to that page it is showing nothing and when refreshed then it is showing so that only am asking how to get it to fetch data and print it when the page is loaded. thanks.
As C.S. says, you don't need to use JSON, just loop through your array and add your list items.
Edited to show basic use of PDO, and demo how to access query results.
The mysql_query extension is deprecated as of PHP 5.5.0, so you I would suggest you use PDO_MySQL
First set up your database connection:
$dbName = "your_database_name_here";
$dbUserName = "your_username_here";
$dbPassword = "your_password_here";
$db = new PDO( 'mysql:host=localhost;dbname='.$dbName, $dbUserName, $dbPassword );
Then run your query, and echo your list (or whatever you need from the database by accessing the $row array within the while loop):
$stmt = $db->prepare("SELECT * from category");
$stmt->execute();
while ($row = $stmt->fetch()){
echo "<li>". $row['title'] ."</li>";
}
p.s. you should get rid of that inline css, it's a bad habit to get into.

variable overwrite when reading from database

I have a meny.php file who loads in with ajax the selected link to the div with id=content as you can see before the footer is included. This code is working and it´s nothing spacial with that. I just think this code may be helpfull.
<!DOCTYPE html>
<?php
include 'includes/head.php';
?>
<body>
<script type="text/javascript">
$(document).ready(function() {
/* This is basic - uses default settings */
$("a.single_image").fancybox();
/* Using custom settings */
$("a#inline").fancybox({
'hideOnContentClick': true
});
/* Apply fancybox to multiple items */
$("a.group").fancybox({
'href' : '#fancybox-inner',
'transitionIn' : 'elastic',
'transitionOut' : 'elastic',
'speedIn' : 600,
'speedOut' : 200,
'overlayShow' : false
});
$("a.single_image").fancybox({
'href' : '#fancybox-inner',
'titleShow' : false,
'transitionIn' : 'elastic',
'transitionOut' : 'elastic'
});
});
$(function(){
// hide the div on page load and use a slidedown effect
$('div.content').fadeOut(0, function(){
$(this).slideDown(500);
});
// capture link clicks and slide up then go to the links href attribute
$('a.slide_page1').click(function(e){
e.preventDefault();
var $href = $(this).attr('href');
$('div.content').slideUp(500, function(){
// window.location = $href;
// alert($href);
});
});
});
</script>
<div class="page">
<?php
include 'includes/header.php';
?>
<div class="container-fluid" id = "bodu">
<div class="row-fluid">
<div class="span12">
<div class="bodu">
<div class="blog">
<div class="container-fluid">
<div class="row-fluid">
<div class="span2" id ="sidebarspan">
<h2>Meny</h2>
<ul id="nav">
<li id ="sidebar">
Hamburgare
</li>
<li id ="sidebar">
Måltider
</li>
<li id ="sidebar">
Dryck
</li>
<li id ="sidebar">
Tillbehör
</li>
<li id ="sidebar">
Desserter
</li>
<li id ="sidebar">
Övrigt
</li>
</ul>
</div>
<div class="span10">
<div id = "content" class="content">
<script src="jquery/general.js"></script>
</div>
</div>
</div>
</div>
</div>
<?php
include 'includes/footer.php';
?>
</div>
</div>
</div>
</div>
<div id="bg-bottom" >
<img src="images/bg-bottom.png" alt="bottom">
</div>
</div>
</div>
</body>
Now for the real problem. Lets say we selected the "hamburgere" from the meny.php. It will then try to load in like I said with ajax the hamburgare.php file. That file looks like this
<?php
include '../includes/head.php';
?>
<h1>Hamburgare!</h1>
<p>hamburgare är gott!</p>
<div class="row-fluid" id = "meals">
<div class="span12" id="right-pane">
<?php
$select = "SELECT * FROM hamburgare";
$sql = mysql_query($select) or die(mysql_error());
mysql_num_rows($sql);
while ($row = mysql_fetch_array($sql)) {
$name = "<name>".$row['name']."</name>";
$price = "<price>".$row['price']."</price>";
$description = "<description>".$row['description']." </description>";
$img = $row['image'];
$event = " <a name='$img' class='single_image' href='fancybox-inner' ><img src='Login/$img'/></a>";
echo $event;
/*closing the whileloop*/
}
?>
</div>
</div>
<div style="display:none">
<div id="fancybox-inner">
<?php
$query = mysql_query('SELECT name FROM hamburgare WHERE image = "' . $img . '"');
?>
<div class="container-fluid">
<div class="row-fluid">
<h2><?php echo $query ?></h2>
<h2><?php echo $name ?></h2>
<div class="span4">
<img src="Login/<?php echo $img; ?>" />
<!--Sidebar content-->
</div>
<div class="span8">
<?php echo $description ?>
<!--Body content-->
</div>
</div>
</div>
</div>
</div>
What I am trying to do here is to load all the images from the hamburgare table in the database and for now i am just printing them out in the while loop in a a-tag. Thats working fine but when a customer clicks on one of the images more specific information shall be shown to the customer.
What happens here is when you click on one of the images the fancybox-inner div is shown but the information the fancybox-inner contains is about the last loaded image. That means it doesn´t matter witch image is selected it will always show information about the last loaded image.
The reason is because the WHERE Clause in the last SQL query, I compare the the last loaded image ($img) who is equal to the one in the database. So what happens is I get this overlapping problem. Also I compare with $img and not the name = $img from the showsen a-tag, who I dont know how to write as a SQL query.
How ever I think the problem is bigger than that. The only thing I do is hiding the fancybox-inner div and just show it when some of the images is selected. That code is stil executed from the start. So I thing I needs Jquery/AJAX to load right information for each image. So I think I need to add onClick for each a-tag in the while-loop.
But since I am new in this stuff I dont know how to write that code. Maybe my hypothesis is wrong as well. I dont know.
What do you guys think? I will appreciate all kinds of help.
thanks in advance
You should add the returned values to an array first and do the output later.
For example:
$formattedResults = array();
while( $row = mysql_fetch_array( $sql ) )
{
$formattedResults[] = array(
'name' => sprintf( '<name>%s</name>', $row['name'] ),
'price' => sprintf( '<price>%s</price>', $row['price'] ),
'description' => sprintf( '<description>%s</description>', $row['description'] ),
'img' => $row['image'],
'event' => sprintf( '<a name="%s" class="single_image" href="fancybox-inner" ><img src="Login/%s"/></a>', $row['image'], $row['image'] ),
);
}
var_dump( $formattedResults );

Tables in Tabbed Panes

I have something like this.
<div id="tabs">
<ul>
<li>Tab 1</li>
<li>Tab 2</li>
<li>Tab 3</li>
</ul>
<div id="tabs-1">
<p>Tab 1 content</p>
</div>
<div id="tabs-2">
<p>Tab 2 content</p>
</div>
<div id="tabs-3">
<p>Tab 3 content</p>
</div>
</div>
Instead of the content (Tab 1 content...) in each of the tabs, I need Tables. The structure of the tables in all the 3 tabs is the same, just the content changes with the MySQL query. The query results may have thousands of rows.
How can I go about it? I am very new to Javascript and PHP
Thanks
What you are trying to achieve is basically:
<div id="tabs">
<ul>
<li>Tab 1</li>
<li>Tab 2</li>
<li>Tab 3</li>
</ul>
<div id="tabs-1">
<table> <!-- Rest of the table1 --> </table>
</div>
<div id="tabs-2">
<table> <!-- Rest of the table2 --> </table>
</div>
<div id="tabs-3">
<table> <!-- Rest of the table3 --> </table>
</div>
</div>
you can use the PHP code to create these tables (I assume you have queried the DB and have the data in variable $rows ):
<div id="tabs-1">
<table>
<?php foreach ($rows as $data)
{
echo "<tr>";
echo "<td>". $data['attribute1'] . "</td><td>". $data['attribute2'] . "</td>"";
echo "</tr>";
}
?>
</table>
</div>
Well, the above code is not complete though, you can add table header, decorate it with CSS etc as per your requirement. But using this method, you will have to repeat the code for each table. Since you said you have the same table structure in all the three tabs, you can create the div tags in a loop it self. In that case you will have:
for(loop as many tabs you want) {
echo "<div id='tabs-'".index.">";
/* Query DB and get the data into $rows variable */
foreach ($rows as $data) {
echo "<tr>";
echo "<td>". $data['attribute1'] . "</td><td>". $data['attribute2'] . "</td>"";
echo "</tr>";
}
echo "</div>";
}
Again, you need to decorate as per your requirements. These code snippets should help you get started!
jQuery UI makes it easy to create and access tabs. It has a nice API for doing the things you describe.

jcarousel and database query

im using a jcarousel:
$(document).ready(function() {
$('#mycarousel').jcarousel({
vertical: true,
auto: 3,
wrap: 'circular'
})
})
so it is a circular config , i have a query like this :
$sql=mysql_query("SELECT * FROM my_insujets, my_inmessages WHERE my_insujets.insujets_idforum = my_inmessages.inmessages_idsujet ORDER BY inmessages_date DESC LIMIT 6");
so the carousel shows 3 rows then the other 3 rows, but after this it keeps on in a loop where nothing is showed.
<div id="wrap">
<ul id="mycarousel" class="jcarousel jcarousel-skin-tango">
while($row=mysql_fetch_array($sql) ) { ?>
<li>
<?php echo $row["message"]; ?>
</li>
<?php } ?>
</ul>
</div>
I do not know if this is the problem, but it appears that your <li> tags are not closed.
I don't see an end </li> in your example code.

Categories