LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   LinuxQuestions.org Member Success Stories (https://www.linuxquestions.org/questions/linuxquestions-org-member-success-stories-23/)
-   -   a beginner's PHP/MySQL script: MySQL in spreadsheet-like layout (https://www.linuxquestions.org/questions/linuxquestions-org-member-success-stories-23/a-beginners-php-mysql-script-mysql-in-spreadsheet-like-layout-776243/)

julianb 12-16-2009 07:48 PM

a beginner's PHP/MySQL script: MySQL in spreadsheet-like layout
 
This was pretty exciting for me when I finally got it to work!

I'm a real newbie at writing PHP/MySQL code. But I successfully took some boilerplate PHP code and modified it into the following.

It lays out your MySQL table as though it were a spreadsheet, where you can type in values.

Totally unfinished... it shows a "submit" button, but that button doesn't do anything at all. So there's no mechanism for data you enter to get back into the MySQL database.

Code:

<?php

mysql_connect("localhost", "root", "pass") or die(mysql_error());
echo "<form action=\"process.php\" method=\"post\">" ;
$db_selected = mysql_select_db('testing123');
//$query = 'SELECT * FROM example';
//$result = mysql_query('SELECT * FROM example');

$result = mysql_query('select * from example');
if (!$result) {  die('Query failed: ' . mysql_error()); }
echo "<table border=0 cellpadding=0 cellspacing=0>";

$y = 0;
$num_fields = mysql_num_fields($result);
$num_rows = mysql_num_rows($result);

while ($y < $num_rows) { $x = 0; echo "<tr>";
    $row = mysql_fetch_row($result);
    while ($x < $num_fields){
    $columnName = "col_" . $x ;
    $value = $row[$x] ;
      echo "<td><input type=\"text\" name=\"$y","_","$columnName\" value=\"$value\"></td>";
    $x++; }
$y++;echo "</tr>";}
echo "</table>";
echo "<input type=\"submit\" value=\"Submit\">" ;
echo "</form>" ;

/* data (column headers should be col_1,col_2,col_3)
row1column1,row1column2,row1column3
row2column1,row2column2,row2column3
row3column1,row3column2,row3column3
*/
?>

<!--

<form action="process.php" method="post">
Your Name: <input type="text" name="name"><br>
E-mail: <input type="text" name = "email"><br>
Location: <input type="text" name = "location"><br>
<input type="submit" value="Submit">
</form>

PS - I'm posting from a CentOS liveCD, first time CentOS user as of a few minutes ago. I figure I'd like to learn to use CentOS, so here I am. Didn't realize that the LiveCD was not intended for a Hard Drive install (!) but I might try to make it install to my hard drive anyway.

carltm 01-01-2010 09:11 AM

Good job working on the php/mysql.

Have you tried installing CentOS? The easy way is to download the
boot.iso image and perform a network install.

kstan 01-01-2010 08:25 PM

congratulation, try to start create some module from some cms (xoops,drupal,joomla) or php framework(zend,cakephp). It will save lot of your time at future.


All times are GMT -5. The time now is 12:00 AM.