Jump to content

Archived

This topic is now archived and is closed to further replies.

kusal

dynamic

Recommended Posts

is it possible to use php to create dynamic web pages

ex ;

if i create a table (let assume that i don't know the attributes) is it possible to print its content

kusal - sri lanka

Share this post


Link to post
Share on other sites
Check out the PEAR package for easely creating dynamic tables.
I'm sure you'll find what you'r looking for.

<a href="http://pear.php.net/package/HTML_Table">http://pear.php.net/package/HTML_Table</a>

Share this post


Link to post
Share on other sites
can any one help me with a code

(thank you for your help "CheesierAngel")

Share this post


Link to post
Share on other sites
kusal, what do you mean dynamic page ?
page=contact, page=news ? Try checking the FAQ, it's a sticky thread.

Share this post


Link to post
Share on other sites
thing is this, i'm developing a inventory control system for a company(as my 2nd year project) and they request that the system sould be able create tables when they needed, using php. then for more complication the tables will be later used to do functions like add, delete, update....(i think you got the point) but i don't have any idea what would be the attributes of those table bcos they enter those details.
so i need a codeing that will take out those attributes from what ever the table and do above mentioned functions

Share this post


Link to post
Share on other sites
There are some CMS's that have this kind of functions integrated as default.
For example cakePHP uses scaffolding... . For wathever table you created in your database
cakePHP will make the forms for adding, editing, deleting your table content automaticly.
The only work left is making the .thtml files for the look en feel of the forms.

Your can read more about this at <a href="http://manual.cakephp.org/chapter/5">http://manual.cakephp.org/chapter/5</a>

Share this post


Link to post
Share on other sites
i can't use 3rd party libaries (it's a project)
pure php

Share this post


Link to post
Share on other sites
so...what is your question - can you do it with php? well since that's your project assignment, then obviously you should know that you can do that in php.  do you have a specific question? do you know [i]anything[/i] about php? if not, you are going to have to start with your first hello world example just like with any other language.

Share this post


Link to post
Share on other sites
yes i know bit of php, i have almost finished my project, but only for the predefiened tables, i don't know a way to get information from tables that i don't know (tables that would be created by the company)
(MAN IT'S VERY HARD EXPRESS MY MESSAGE).

kusal

Share this post


Link to post
Share on other sites
maybe it would help if you gave an example.  are you talking about dynamically creating database tables through php? if so, then this is not really a php question, but a database (example: mysql) question.  You would use the same commands (for example) for creating a table as you would in your database. As far as php, you'd just use the variable name instead of an actual name. 

example using mysql:
[code]
<?php
  $tablename = $_POST['tablename']; //name of table user entered from some form
  $columnname = $_POST['columnname']; //name of some column user entered from some form

//example: create the table with one column that's varchar(30) type, default not null
$sql = "CREATE TABLE $tablename ($columnname varchar(30) NOT NULL)";
$result = mysql_query($sql);

?>
[/code]

Share this post


Link to post
Share on other sites
Search for sql functions to discover how your tables and which tables are build in your database.
MySQL has his own functions for determining which tables there were defined in your database, which columnheaders they have, the kind of data stored in the different columns and so on.

You better first check out the MySQL functions you can execute from your php script to receive the needed data.
With this data you can recreate your tables dynamicly with php.

For example:
query "show tables" and store it into a var.
foreach(var -> table)
    query desc table
    recreate the table
    query for all content in the table
    output your data

It is for your project so i'll let you write the code yourself, but i want to give you the 'thinking' solution. (Which is for most cases the most difficult part)

Share this post


Link to post
Share on other sites
Yes you got the point(but not fully).

yes i know that in that way i can create tables,

let assume that the above created table needed to insert data (assume that i have given the system to them)then i need to have created a page to add those details in the newly created table.

but the problem is, i don't know any details about that table attribute, how can i dynamicaly get those table details to create a adding page

Share this post


Link to post
Share on other sites
I still don't understand what you are asking.  If you have input fields for the user to input a table name, column name and column type, you would create the table based off of the inputed information.  Are you now asking something like this: User wants to go to a page and there will be a list of the tables he just created, and it lists what type of column it is, so he knows what kind of info he can put in it?

Share this post


Link to post
Share on other sites
Can you give a specific example for what it should be used for ?
Do you already have some code we can see ?

Share this post


Link to post
Share on other sites
assuming that you are asking what i mentioned in my previous post - again, this is not really a php question. but here is an example of the sql query and how you would utilize it with php:

[code]
<?php
//obviously you need to connect to the db first. this also
//assumes you are using mysql, seeing as you have yet
//to mention what db you are using...

$tablename = 'nameoftable'; //put name of table here

//this query string will return a list of all the columns in the
//table and the information about the columns
$sql = "show columns from $tablename";
$result = mysql_query($sql) or die(mysql_error());

//fetch and make an array of the results of the query
while ($rs = mysql_fetch_array($result, MYSQL_ASSOC)) {
  $list[] = $rs;
}

//example of listing out the results:
foreach ($list as $row) {
  echo "<b>-- {$row['Field']} -- </b><br>";
  foreach ($row as $key => $val) {
      echo "$key : $val <br>";
  }
}
?>
[/code]

Share this post


Link to post
Share on other sites
He means have something to where tables can auto-generate themselves using php with proper attributes based on the situation.

Share this post


Link to post
Share on other sites
the problem is not how to create a table or retrieving data from it.

ex:

the project is that i create an invetory control system for computer hardware devices for a mobile telephone company in sri lanka. the company has pc,routers,servers,laptops,printer,telephones.....etc,  to be stock controlled. so i have created tables for every device with there uniqe attributes. and created funtions like add,delete,update,view...etc, for every table using php and mysql

NOW THE PROBLEM IS THAT COMPANY IS ASKING ME TO CREATE A FUNCTION THAT, IF THEY COME ACROSS NEW HARDWARE DEVICE (EX: ISP LOAD BALANCER, WHICH IS NOT CREATED ABOVE) THEY NEED TO CREATE A TABLE FOR IT AND NEED TO HAVE ALL ABOVE MENTIONED FUNCTIONS.

HOW CAN I DO THIS, I NEED TO CREATE HTML PAGES AND PHP PAGES DYNAMICLY FOR A CHANGING DATABASE.

Share this post


Link to post
Share on other sites
Please don't use capital letters, it doesn't sound nice to me :(
Bold those words would be better if you want people to take note of that parts.

Hmm.. You could store the informations in database, then loop them out.
Take a look at this page, you can store all those items then loop them out,
hope I got what you mean.

Share this post


Link to post
Share on other sites
I had to do something similar for a cell phone store, except it was with manufacturers of the phones.

One of the best tips I got was to create a seperate table just for differn't categories or product types.

Ie in the table would just be
Servers
Desks
Monitors
Phones etc.

I then would while loop all of these out and it created a list of items for them to choose...

So say they choose servers, it would go to details.php?type=servers for example.

Before hand you have to figure out some of the things they have in common, so are you getting into specific detail with each object or not? If not then you can do statics of such things like quantity, location, year purchased, etc.

Then the script loops through your database and outputs out any "items" that are categorized as a server to a new row in your table, then you have standard functions such as add or subtract them.

Then later on you make an admin panel so if they add in SUPER SERVERS then all it does is create the new category and they have the option to add products as Super Servers.


Not sure if this helps, if it doesn't help hopefully it will help you solve some questions.

-Sean

Share this post


Link to post
Share on other sites
can i use [b]show table[/b] and [b]describe[/b] table_name to get the info,

let say that i get those info but how can i create relevant html page and php pages.

Share this post


Link to post
Share on other sites
Eh.. Can you explain to me what does you mean by [b]show table[/b] and [b]describe[/b] ?

Try this sample, hope it's what you looking for.

MySQL table structure:
[code]CREATE TABLE `handphone` (
`id` MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`type` TEXT NOT NULL ,
`servers` TEXT NOT NULL ,
`desks` TEXT NOT NULL ,
`monitors` TEXT NOT NULL ,
`phones` TEXT NOT NULL
)[/code]

MySQL datas:
[code]INSERT INTO `handphone` VALUES (1, 'type1', 'servers1', 'desks1', 'monitors1', 'phones1');
INSERT INTO `handphone` VALUES (2, 'type1', 'servers2', 'desks2', 'monitors2', 'phones2');
INSERT INTO `handphone` VALUES (3, 'type1', 'servers3', 'desks3', 'monitors3', 'phones3');
INSERT INTO `handphone` VALUES (4, 'type2', 'servers1', 'desks1', 'monitors1', 'phones1');
INSERT INTO `handphone` VALUES (5, 'type2', 'servers2', 'desks2', 'monitors2', 'phones2');
INSERT INTO `handphone` VALUES (6, 'type2', 'servers3', 'desks3', 'monitors3', 'phones3');
INSERT INTO `handphone` VALUES (7, 'type3', 'servers1', 'desks1', 'monitors1', 'phones1');
INSERT INTO `handphone` VALUES (8, 'type3', 'servers2', 'desks2', 'monitors2', 'phones2');
INSERT INTO `handphone` VALUES (9, 'type3', 'servers3', 'desks3', 'monitors3', 'phones3');[/code]

Scripts
[code]<?php
mysql_connect("localhost", "root","") or die(mysql_error());
mysql_select_db("hp") or die(mysql_error());
if (isset($_GET['type'])) {
$result = mysql_query("SELECT * FROM handphone WHERE type='".$_GET['type']."'") or die("Query failed: " . mysql_error());
    while($row = mysql_fetch_assoc($result)) {
        ?>
        <table width="560" border="1" align="center" cellpadding="2">
  <tr>
    <td><strong>Servers: </strong><?php echo $row['servers']; ?></td>
  </tr>
  <tr>
    <td><strong>Desks: </strong><?php echo $row['desks']; ?></td>
  </tr>
  <tr>
    <td><strong>Monitors: </strong><?php echo $row['monitors']; ?></td>
  </tr>
  <tr>
    <td><strong>Phones: </strong><?php echo $row['phones']; ?></td>
  </tr>
</table><br>
        <?php
        }
} else {
echo "You didn't select any type.";
    }
?>[/code]

If you save the code in index.php, then you can access it from http://domain/index.php?type=1 or type=2 or type3
It will display according to your type.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.