PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old May 15th, 2008, 06:46 AM
bidClick bidClick is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 2 bidClick User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 40 sec
Reputation Power: 0
Tables within php

hey guys, having a problem with the following code, you choose a table from the database and then it displays all the fields below, which is fine, it all works well, however, it displays them all horizontally as if its just one row, how can i do it it vertically, so the field name is in column one and the value is in column two and each entry is another row going down?


PHP Code:
// Initial connection to the database
$database mysql_connect("localhost""admin""password");
mysql_select_db("database");

// Because the first time we enter the site we have'nt selected
// a table to view we init the number of rows in the current table
$rowNum=0;

?>



<?

// Get the data of the tables on the scheme
$result mysql_list_tables ("database");

$i=0;
while (
$i mysql_num_rows ($result))
{
    
$tb_names[$i] = mysql_tablename ($result$i);$i++;
}

// Check if we got here after pressing submit on the page
if (isset($_POST['submit']))
{
   
// If we did press the submit button, we sould view the table that was on the select
   // box
   
$submit=$_POST['submit'];
   
$table=$_POST['table'];
}
else
{
    
// If we didn't get here after pressing the submit button, check if we already
    // viewed one of the tables (and saved it's name on the hidden field hidtable
       
if (isset($_POST['hidtable']))
    {
        
$table $_POST['hidtable'];
    }
    else
    {
        
$table="";$xnum=0;
    }
}

?>
    </p>
</div>
<center>

<form name=ff method=post action="edit.php">
  <p><br>
    <b>Select Client</b>:      
    <select name="table">
      <?
for($x=0;$x<$i;$x++)
{
?>
        <option value="<? echo $tb_names[$x];?><? if (isset($table) && $table==$tb_names[$x]) {echo " selected ";} ?>><? echo $tb_names[$x];?></option>
      <? }?>
    </select>
      <input type="submit" name="submit" value="Submit" id="submit">
    
    
    
      <?

// Check if we pressed the submit button and if we did - fetch the table data
if(isset($submit) || isset($_POST['hidtable'])){
$SQL="SELECT * FROM $table";
$result mysql_query($SQL);
$xnum mysql_num_fields($result);
$rowNum mysql_num_rows($result);

// Read all the data in the table
for ($j 0$j<$rowNum; ++$j)
{
    
$row mysql_fetch_array($result);
    
$currTable[$j]=$row;
}
}




echo 
"<p>";?>
    </p>
  <center>
<table bordercolor="#FFFFFF" bgcolor=#FFFFFF>
  <tr>
<td bordercolor="#FFFFFF" bgcolor="#FFFFFF"><span class="style1">Offset</span></td>
<td bordercolor="#FFFFFF" bgcolor="#FFFFFF"><span class="style1">Field Name</span></td>
<td bordercolor="#FFFFFF" bgcolor="#FFFFFF"><span class="style1">Field type</span></td>
  </tr>


<?
// Get the data about the primary keys and the numeric fields in the table
for($x=0;$x<$xnum;$x++)
{
    
$name[$x]=mysql_field_name($result,$x);$type[$x]=mysql_field_type($result,$x);
    
$currField mysql_fetch_field($result,$x);
    
$key[$x]=$currField->primary_key;
    
$numeric[$x]=$currField->numeric;
?>

<? }?>
</table>
  </center>
<br>

<?

// Check to see if there was an update to a row
for ($j 0$j<$rowNum; ++$j)
{
    if (isset(
$_POST["update".$j]))
    {
        
// Make an sql update query
        
echo "<center>There was an update to row $j</center>";
        
$sql="update $table set ";
        for (
$i 0$i $xnum; ++$i)
        {
            if (
$numeric[$i]==1)
            {
                
$sql.=$name[$i]."=".$_POST["$name[$i]".$j]." ";
            }
            else
            {
                
$sql.=$name[$i]."='".$_POST["$name[$i]".$j]."' ";           
            }
           
            if (
$i != $xnum-1) {$sql.=",";}
        }
        
$sql.="WHERE ";
        
$notFirstKey 0;
        for (
$i 0$i $xnum; ++$i)
        {   
            if (
$key[$i]==1)
            {
                if (
$notFirstKey == 0) {$notFirstKey=1;}
                else {
$sql.=" AND ";}
                
$sql.=$name[$i]."=".$currTable[$j][$i];
               
            }
           
        }
        if (
$notFirstKey == 0) {
            echo 
"Table does not have a primary key, not doing anything";
        }
        else {
              echo 
$sql;
            
$result mysql_query($sql);
        }

       
    }
}

// Check to see if there was a delete to a row
for ($j 0$j<$rowNum; ++$j)
{
    if (isset(
$_POST["delete".$j]))
    {
        echo 
"<center>There was an delete to row $j</center>";
        
$sql="delete from $table ";
        
$sql.="WHERE ";
        
$notFirstKey 0;
        for (
$i 0$i $xnum; ++$i)
        {   
            if (
$key[$i]==1)
            {
                if (
$notFirstKey == 0) {$notFirstKey=1;}
                else {
$sql.=" AND ";}
                
$sql.=$name[$i]."=".$currTable[$j][$i];
               
            }
           
        }
        if (
$notFirstKey == 0) {
            echo 
"Table does not have a primary key, not doing anything";
        }
        else {
              echo 
$sql;
            
$result mysql_query($sql);
        }
       
    }
}

// Check to see if there was an insert of a row
    
if (isset($_POST["insert"]))
    {
        echo 
"<center>There was an insert of a row </center>";
        
$sql="insert into $table values (";
        for (
$i 0$i $xnum; ++$i)
        {
            
$sql.="'".$_POST["$name[$i]"."insert"]."' ";
            if (
$i != $xnum-1) {$sql.=",";}
        }
        
$sql.=")";
          echo 
$sql;
        
$result mysql_query($sql);
       
    }




///////////////







if(isset($submit) || isset($_POST['hidtable'])){
$SQL="SELECT * FROM $table";
$result mysql_query($SQL);
$xnum mysql_num_fields($result);
$rowNum mysql_num_rows($result);

}

?>




<center>
  <table bgcolor="#FFFFFF"><tr><td bgcolor="#e3e3e3" font="red"><table bgcolor="#FFFFFF">
    <tr>
      <?
for ($i=0$i<$xnum; ++$i)
{
$name[$i]=mysql_field_name($result,$i);
?>
      <td bgcolor="#e3e3e3" font="red"><? echo $name[$i]; ?></td>
      <? ?>
    </tr>
    <? for ($j 0$j<$rowNum; ++$j)
{
$row mysql_fetch_array($result);
$currTable[$j]=$row;
}
?>
    <? for ($j 0$j<$rowNum; ++$j)
{
?>
    <tr>
      <? for ($i=0$i<$xnum; ++$i)
{
?>
      <td><input name=<? echo "\"$name[$i]".$j."\""?> type="text" id=<? echo "\"$name[$i]\""?> value=<? $currRow=$currTable[$j]; echo "\"$currRow[$i]\""?> /></td>
      <? ?>
      <td><input type="submit" name=<? echo "\"update".$j."\""?> value=<? echo "\"update\""?> height="10" />      </td>
    </tr>
    <? }
for (
$i=0$i<$xnum; ++$i)
{
?>
    <? ?>
  </table></td>   
</tr>
</table>

</center>
<input type="hidden" name="hidtable" value=<? echo "\"$table\""?>>


</form> 

Reply With Quote
  #2  
Old May 15th, 2008, 06:59 AM
DavidMR's Avatar
DavidMR DavidMR is online now
Contributing User
Click here for more information.
 
Join Date: Apr 2007
Location: Ireland
Posts: 711 DavidMR User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Week 4 Days 13 h 52 m 42 sec
Reputation Power: 2
to display them vertically you can either add a line break "<BR>" after each field, or use HTML Rows in the table "<TR>"

like :

PHP Code:
 $name[$i]=mysql_field_name($result,$i);
?>
      <td bgcolor="#e3e3e3" font="red"><? echo $name[$i]; ?></td>
      <? ?>
    </tr> 


would become:
PHP Code:
 $name[$i]=mysql_field_name($result,$i);
?>
      <TR><td bgcolor="#e3e3e3" font="red"><? echo $name[$i]; ?></td></TR>
      <? ?> 
__________________
When I die, I want to go peacefully like my Grandfather did, in his sleep -- not screaming, like the passengers in his car.

Reply With Quote
  #3  
Old May 15th, 2008, 08:32 AM
bidClick bidClick is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 2 bidClick User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 40 sec
Reputation Power: 0
unfortunatly that only puts the fields in the column, not the values, they are staying horizontal and i can't figure out how to make them the same

Reply With Quote
  #4  
Old May 15th, 2008, 08:34 AM
DavidMR's Avatar
DavidMR DavidMR is online now
Contributing User
Click here for more information.
 
Join Date: Apr 2007
Location: Ireland
Posts: 711 DavidMR User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Week 4 Days 13 h 52 m 42 sec
Reputation Power: 2
you would need to do what i did to your code to the other table, where you loop with <TD>, you can change it to loop with <TR><TD> etc, dont forget to include the closing tags...

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > Tables within php


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway