Render a HTML page using MySQL (Dynamic lines) [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am wondering how I would render a HTML page with MySQL.
I want to render a page that has different lines, for different rows in the table.
The table is:
I want the web page to render like this:
Obviously, if there are less rows I don't want as many rows to display, and same if there are more.

Purely because it's late at night here's some free code.
Please notice two things.
1. I'm using mysqli not mysql.
2. I wrote this from memory, I researched on the web how to write this. I have no degree, no professional job as a programmer but I all found this out by myself by researching and browsing SO. Please next time research and try out different things for yourself.
<?php
$host = 'localhost';
$password = '';
$user = '';
$database = '';
// Link for the connection to MySQL
$link = mysqli_connect($host,$user,$pass);
// If statement to check if the link has been succesful if not, give error. If it is succesful select database.
if(!$link)
{
if (mysqli_connect_errno())
{
echo "Connection unsuccessful<br/>";
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
}else
{
mysqli_select_db($link, $database);
}
$query = "SELECT * FROM [table]";
$result = mysqli_query($link, $query);
echo '<table>';
while($row = mysqli_fetch_assoc($result))
{
echo '<tr>';
echo '<td>'. $row['CSTEAMID'] .'</td>'
echo '<td>'. $row['OSTEAMID'] .'</td>'
echo '<td>'. $row['TIMEJOINED'] .'</td>'
echo '</tr>';
}
echo '</table>';
?>

Do you have any code already?
Pseudo code for what you want:
Write html for a standard page
In the body somewhere, use PHP (or other scripting language) to connect to your mysql db and retrieve the needed data.
Output with the PHP in an HTML tabular format

Related

PHP and MySQL using xampp as web server [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
How can I insert data from a table to another table after the clicking of a button using PHP and MySQL?
Following php script can be used to fetch values from one Table and insert data into another table:
<?php
$servername = "localhost"; //if running on localhost
$username = "root"; // if running on localhost
$password = ""; // empty for mysql default password
$dbname = "database name";
$conn = mysqli_connect($servername, $username, $password,$dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if (isset($_POST['name of button'])) { // if button is clicked the following code will be executed
$sql_db1 = "SELECT * FROM `table_name_1` WHERE condition"; // if you want to select data based on some constraint.
$sql_db1_query = mysqli_query($conn,$sql_db1); // mysqli_query takes 2 parameters -> connection to database and sql query.
while($row = mysqli_fetch_array($sql_db1_query)){
$sql_db2 = "INSERT INTO `table_name_2`(`column1`,`column2`,`column3`,`column4`) VALUES($row['column1'],$row['column2'],$row['column3'],$row['column4'])"; //'column1' inside $row[] and so on should be replace by the column names of the table in which you want to insert the data
$sql_query_db2 = mysqli_query($conn,$sql_db2);
}
}
?>
I think this takes a little more than a simple answer, try reading a php-mysql tutorial:
https://www.w3schools.com/Php/php_mysql_intro.asp
In plain Mysql you just need a "insert into" statement with a select, as explained here. Then you'll just need to create a php statement to execute this SQL.

Dynamic Drop Down List using loop [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have database named as 'ecc' with table named as 'client'
table client has many fields, consider one of them named as ProjectManager
I want to display data of ProjectManager in select drop down list.
More data can be added to ProjectManager so loop is necessary.
please can some one help by writing the code for the same...
thanks for help in advance..
Below is a sample code for mysqli:
I did not given that a try but should be working. You need to replace values for username, password for db and what value you need in your select options
<?php
// Connect db
$mysqli = new mysqli('localhost','username','password','ecc');
// check if error
if ($mysqli->connect_error) {
die('Can not connect to DB error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
// MySqli Select Query
$results = $mysqli->query("SELECT ProjectManager FROM client");
echo '<select name="project_manager">';
echo '<option value="">-select project manager-</option>';
while($row = $results->fetch_assoc()) {
echo '<option value="'.$row['ProjectManager'].'">'.$row['ProjectManager'].'</option>';
}
echo '</select>';
// Frees memory
$results->free();
// close connection
$mysqli->close();
?>

How to edit the value updated in a database from the form using php and mysql [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want to edit the data which is already in the databse.
Suppose I have updated my database, but after an year the user who has updated that database want to edit something like his address which is changed now through the form, then how can we do that. I am posting
<?php
// Create Local variable
$taken = "false";
$database = "railway";
$password = "";
$username = "root";
// Main if statement
//if($userreg && $passreg){
// Connect to database
$con = mysqli_connect('localhost', $username, $password,$database);
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
---------
WHAT TO WRITE IN BETWEEN
----------------
mysqli_close($con);
Suppose my table name is Railway and the attributes are time, name, station_to and station_from. I want to change the name.
Please write both the form as well as php and mysql query.
It would be something like this:
$sql = "UPDATE Railway SET name='Joe' WHERE id=5";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}

How to display stored url in database to a webpage? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am new to PHP and MySQL. I've created a form that uploads a url to my database field and I would like to know how to display the stored url from the database to a webpage.
Like this
$query = "SELECT url FROM table";
$resource = mysql_query($query);
if (mysql_num_rows($resource) > 0) {
$results = array();
while($record = mysql_fetch_array($resource)) {
$results[] = $record;
}
}
foreach ($results as $result) {
echo $result['url'];
}
And if you are among the mysql_* complain group:
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$query = "SELECT url FROM table";
if ($result = $mysqli->query($query)) {
// Loop through results
$results = array();
while ($row = $result->fetch_assoc()) {
$results[] = $row;
}
$result->free();
}
foreach ($results as $r) {
echo $r['url'];
}
$mysqli->close();
Edit I would like to add that it would not be a bad idea to first consult a beginners PHP/MySQL book and get yourself up to speed with the programming basics.
Resource
PHP Beginner books
As we don't know your database structure or the used PHP library, I assume that you can select the stored information into an array like this:
$row['URL'] = 'your stored url';
Then you can easily print it out using more formulas:
<?php print('Click here'); ?>
Or if you want to integrate php only for the url printing:
Click here
Basically the two is identical, but if you have a large HTML and minimal PHP, then the second solution would be easier to edit and maintain.

How do i make a list of items that takes data from a MySQL database [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have an HTML/CSS "contentbox"(It's basically a box that includes an image, title, description, name, etc of a product or item) and i have a database table with all this information for a lot of items/products.
I want to take all this data from product1(title, image, description) from the database and put it in my contentbox.
And i want to do this with all my products from my table and display all this contentboxes below each other. Sort of like the craigslist for sale ads that are in a list.
How do i do this? do i need javascript or where do i start? I don't know much about programming and i'm learning as i go.
I realize i should put some code in my questions so here goes another question:
I have a div container that looks like this:
<div class="contentBox">
<div id="column1">
<img src="images/gallery/girlthinking.jpg" alt="" id="imagen">
</div>
<div id="column2">
<p class="tituloanuncio"><b>Titulo del anuncio</b></p>
<p class="descripcionanuncio">Descripcion kfewoijfew</p>
</div>
<div id="column3">
<p class="precioanuncio"><b>$1000</b></p>
<p class="contactoanuncio"><b>Contacto<br></b>DueƱo: Alejandro<br>Telefono: 8331578460<br>jorgegilcavazos#gmail.com<br>Facebook</p>
</div>
</div>
It works in html, i want to replace the default values a put in there with data from my table, for example where it says.
<div id="column2">
<p class="tituloanuncio"><b>Titulo del anuncio</b></p>
<p class="descripcionanuncio">Descripcion kfewoijfew/p>
</div>
I would like to replace "titulo del anuncio" with the column 'anuncio_titulo' and replace "descripcion kfewoijfew" with the column 'anuncio_descripcion'.
I have this php code that displays all data from the 2 previus columns i mencioned
$result = mysqli_query($con,"SELECT * FROM anuncio");
while($row = mysqli_fetch_array($result)) {
echo $row['anuncio_titulo'];
echo $row['anuncio_descripcion'];
}
How do i echo my initial html div container replacing the default text i put with the new columns?
I hope this makes sense.
You need to first fetch the data from database (read about MySQL, mysqli PHP function). Then you need to pass the data to the site that user sees using PHP (read about PHP). Finally you need to display the data with HTML (read about HTML with PHP).
Nice explanation of mysqli functions is here. To fetch data:
Connect
Create query
Execute query
Get data from query
First connect:
$db = new mysqli('localhost', 'user', 'pass', 'demo');
// check if connection succeeded
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
Then create and execute query:
$sql = "SELECT * FROM `users` WHERE `live` = 1"
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
and get data from query
$usernames = array();
while($row = $result->fetch_assoc()){
$usernames[] = $row['username'] . '<br />';
}
Once you have an array, pass it to your view and print, for example as a part of a table:
<table>
<?php foreach($username in $usernames): ?>
<tr><td><?php $username ?> </td></tr>
<?php endforeach;?>
</table>
If you do not need to change data without reloading the page, you don't need javascript or ajax. I can see several ways you can do what you intend to, but will just explain the easiest one.
First is to assign the text to the variable and print it. If it needs to be changed with data from databse then just assign the value to that variable.
<?php
$title = "Titulo del anucio";
$desc = "Descripcion kfewoijfew";
// it has default values, let's say you want to fill them with data from db now
if(/* some condition here, like data from GET : $_GET['change_data'] == true or something */) {
$db = new mysqli('localhost', 'user', 'pass', 'demo');
// (...) and so on like I wrote above, query the data and assign to value
$res = $result->fetch_assoc() // if you know there is one result you can shorten it
$desc = $res['anuncio_descripcion'];
$title = $res['anuncio_titulo'];
}
?>
<div id="column2">
<p class="tituloanuncio"><b><?php echo $title;?></b></p>
<p class="descripcionanuncio"><?php echo $desc?>/p>
</div>

Categories