How to allocate one ID to another and show this in HTML - php

For my iPhone web app I have created a database in php MyAdmin, it contains two tables (bookings, waiters and allocations) the I wish to view the bookings within a table in HTML where i can allocate a waiter to that booking. Any tips on how this would be done, any help will be appreciated as this is my first contact with web app development and MyAdmin. Thanks!

Mysql work with autoincrement id. You will have an id after of an insertion. You can to create a table only to id's and after to use it in the insertion of the waiter.

First you will need to get the data from the table via MySQL
$sql = "SELECT * FROM table_name";
$result = mysql_query($sql);
Then you would loop through the result
while($row = mysql_fetch_assoc($result)){
extract($row);
}
Then you can do as you wish with the returned data...
You really need to give us a little more... What have you got so far?

Related

MySQL update value +1 (increment) on button click

I know this question has been answered over a million times, but I'm very new to the whole programming scene and I just can't get it to work. Sorry, I hope you can help me!
HTML:
<div class="the_score">+76</div>
<div id="increment_value">Like</div>
I have a MySQL connection and I can fetch the data, but I want the score to update with +1 when someone clicks the Like div. I have a table "websites" with row "WebScore" that has the function int(10) (?? I hope that is correct).
I use this to fetch the data and show it on my website:
$r = rand(2,3);
$sql = "SELECT * FROM websites WHERE ID = $r";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
I presume you have a column, not a row, called WebScore.
You need the query
UPDATE websites SET WebScore = WebScore + 1 WHERE ID = $r
As you can see, it finds the right row of your table and updates the value of the WebScore column.
This sort of things can only be done from a PHP program. It can't be done directly from a Javascript method invoked within a web browser. So, if you want to react to a user click, you'll need to post a form or invoke an Ajax style call to a php endpoint. How to do that is the province of another question.
Keep plugging away; you'll figure out this database stuff.

How to store mysql column info in an array?

The site I am creating will allow users to update their status. The only reasonable way I could think of storing the information would be to post a new line to my database for every post they make. I can associate the posts with their usernames on the same line. The problem is that no matter what I try nothing works on retrieving the info. I need to select the column where their status will be held and store all the status's in an array. Is this even possible? I am using PHP. As all the code I have used has been very ineffective I will not include it. Any help would be VERY appreciated!
Try to use:
<?php
$q = $db->prepare("DESCRIBE tablename");
$q->execute();
$result = $q->fetchAll();
var_dump($result);

Populate several HTML Divs with PHP/MySQL and JavaScript

I've noticed several threads on here and through Google searches on how to do this, but I need something a little more specific to my needs in order to execute it as I am pretty new to PHP. Hopefully someone can help.
I have a page with 5 Divs (lets just call them 1-5). I have a MYSQL database named 'Users' with columns 'ID' (primary key), 'Member', 'Review' and 'location'.
I would like to randomly populate each Div with rows from this DB and show information from the above mentioned columns.
Can anyone give me guidance on where to begin, or possibly give me sample code that meets the above criteria?
Thanks so much for any help.
Here is a general answer:
1) in PHP establish a connection to you database. something like this:
$connection = mysql_connect($host,$username,$password);
2) query the database:
mysql_select_db($schema, $connection);
$result = mysql_query("Put your sql query here...");
3) loop over the results and echo them to the screen,
while($row = mysql_fetch_array($result))
{
echo $row['mysql_column_name_here'];
}

AutoUpdate the last five entrys of a table in a MySQL database

On this page you see at the bottom a "real time" updates of a twitter search with a very simple but elegant animation, i want to do the same but showing the last five entrys on my MySQL data base, is out there a good way to do this?, maybe a combination of php and jquery?
Thanks for any help!
Yes, it's pretty straightforward.
Server side:
Create a PHP page which selects the latest 5 rows from your database and outputs them as JSON.
//connect to db
$result = mysql_query("SELECT * FROM table ORDER BY some_timestamp_column DESC LIMIT 5");
$data = array();
while ($row = mysql_fetch_assoc($result)) {
$data[] = $row;
}
echo json_encode($data);
Client side:
Every X seconds, make an AJAX request to your PHP script to get the 5 newest rows. Compare them to the ones you've already displayed and add the newer ones to the top of the 'update area'. You can use jQuery to do this.

How to create HTML tables from MySQL? [duplicate]

This question already has answers here:
Creating dynamic tables in HTML using MySQL and PHP
(2 answers)
Closed 15 days ago.
I'm building a part of a system where the user can define "views" from a mysql database.
I want some way of generating simple HTML tables/reports from the data, but not just plain output of sql queries- simple joins probably isn't enough.
Any ideas?
Just to be clear: I know perfectly well how to create a HTML table with PHP/MySQL - that is the simple part. What I want to do is allow my USERS to create their own tables without being exposed to the process!
Right, i'll give some more details- thanks for all the responses:
Currently users can create tables using a wizard, then view/edit/delete records in these tables. This makes it difficult to give an example as tables could change. The data I expect the client to input is for renting houses- a table of houses, tenants, rooms, maintenance issues etc.
What I aim to enable the user to do is to create some basic formatted output of a particular view, for example "show rent turnover in the current month for all houses" or "show overdue tenants" or "show rooms with no tenants". Once set up, these will only be viewed, not edited, so the process just has to be bearable to set up.
Does that make sense? If it's not possible I'll just hard-code the views- just makes the system less flexible!
Is this what you're looking for? It will dynamically generate an html table from a mysql result no matter what or how big the result.
//this function dynamically outputs a basic HTML table from any mysql result
function createTable($result) {
$table = '<table><tr>';
for ($x=0;$x<mysql_num_fields($result);$x++) $table .= '<th>'.mysql_field_name($result,$x).'</th>';
$table .= '</tr>';
while ($rows = mysql_fetch_assoc($result)) {
$table .= '<tr>';
foreach ($rows as $row) $table .= '<td>'.$row.'</td>';
$table .= '</tr>';
}
$table .= '<table>';
//mysql_data_seek($result,0); //if we need to reset the mysql result pointer to 0
return $table;
}
This is how you'd print the table:
echo createTable($result);
You can use phpMyAdmin to show MySQL in nice tables or if you want to code it yourself you can use PHP and HTML to do the work:
<table>
<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error());
mysql_select_db("mydb");
$result = mysql_query("SELECT id, name FROM mytable");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<tr>
<td><?=$row['id']; ?></td>
<td><?=$row['name']; ?></td>
</tr>
<?
}
mysql_free_result($result);
?>
</table>
Edit: After reading your edits, it may not be the best idea to make users "create their own tables", but instead to give them a number of options and sort orders to create a better user experience. Otherwise it's just a form with some input elements and some if statements.
You'll have to expose users to part of the process, since they have to describe the tables and table relationships somehow. You could take a page from the likes of Crystal Reports, Visual Studio and phpMyAdmin and create a visual table designer. A table is represented as a rectangle with a title bar showing the table's name and a list of fields. The tables are on a canvas, and can be placed anywhere on that canvas. Foreign keys are represented as lines connecting fields.
You can use the same interface as a query creator: the user specifies which tables to query by dragging them from a list of tables to the canvas, which fields they're interested in (perhaps include a checkbox next to each column) and which fields to join on by connecting fields with lines. To join a table to itself, you could allow a table to be added to a query more than once, or allow fields within a table to be connected to other fields within the same table.
If you don't have it, grab phpMyAdmin, install it, open a database and go to the "Designer" tab to see how it works. You can also take a look at some screenshots
You have to define the way users will create those HTML tables. Do they setup a number of rows and columns, then feed the table manually ?
Once we had to do a templating system where all images, css properties, positionning of various html blocks, etc. The whole system was 5 or 6 mysql tables just to store the templates properties, but if it's just a table you can ease the process.
Imagine one mysql table for table format and properties (row count, col count, col width, colors, etc.) and another mysql table for data to put inside. This second table should have columns for storing date, row and col positions.
The rest is all making a form accessible to non-tech users to configure and feed table, and render table with some PHP loops and queries.
Approximately 100% of websites does the same task: create HTML tables from SQL database.
So, you can get yourself some PHP/Mysql book to get an idea.
Though all tables being hardcoded by a programmer.
As for the user-defined views it can be a bit more complicate. But possible too
<?php
$query="SELECT * FROM table_name";
$result=mysql_query($query);
$numfields = mysql_num_fields($result);
echo "<table border=1><tr>";
for ($i=0; $i < $numfields; $i++) // Header
{ echo '<th>'.mysql_field_name($result, $i).'</th>'; }
echo "</tr>";
while
($res=mysql_fetch_row($result)) //you may use mysql_fatch_array($result) which can hendel both $res[name] and $res[1]
{
echo"
<tr>
<td> $res[0]</td>
<td> $res[1]</td>
<td> $res[2]</td>
.
.
.
</tr>";
}
echo"</table>";
?>
100% Working just changed.
$query variable according to your table

Categories