Solved

what to use explode or implode after  48 line

Posted on 2009-06-26
20
199 Views
Last Modified: 2012-05-07
hello sir ,
just see the 48 to 55 lines.
posting the full code.

the 2 variables $fieldClause and $valueClause conatins the whole values, so we need to explode it but how ??

i just simply echoed the the two variables in a loop , it worked but the fields shown are in 1 row. i think we need to explode both the varaibles and then display in diferent td's like the usual way we display our records.so, please can u tell me how should i do it.

view source of the output is like this,

<table border="1" bgcolor="#FF0000"><tr><td colspan="2">Table name is app</td></tr>
                        <tr>
                        <td>id, name, address, d1, d2, d3</td><td>'7', 'dfgdf', 'dfg', 'cbv', 'cb', 'cvb'</td>
                        </tr>
                        </table>

and the output is given in a below snapshot.

but the output should be like this,

<tr>
      <td>id</td><td>7</td>
</tr>
<tr>
      <td>name</td><td>dfgdf</td>
</tr>
like this go on


<?php

session_start();

 

$host = "localhost";

$username = "root";

$password = "";

$database = "cmsbuilder";

 

$con = mysql_connect($host,$username,$password) or die("could not connect".mysql_error());

$db = mysql_select_db($database,$con);

 

$sql="select * from ".$_SESSION['tname'];

$query=mysql_query($sql) or die("could not find the table".mysql_error());

$totalfields=mysql_num_fields($query);

//echo $_SESSION['tname'];

$sql1 = "select * from field_types where tablename='".$_SESSION['tname']."'";

$query1 = mysql_query($sql1) or die("could not execute".mysql_error());

 

//$i=1;

$fieldClause="";

$valueClause="";

for($a=1;$a<=$totalfields;)

        {

                while($record = mysql_fetch_assoc($query1))

                {

                        if($record['fieldtype']=='textbox'){

                        $fieldClause.=$record['fieldname'].", ";

                        $valueClause.="'".$_REQUEST['text'.$a]."', ";

                        }

 

                        if($record['fieldtype']=='wysiwyg'){

                        $fieldClause.=$record['fieldname'].", ";

                        $valueClause.="'".$_REQUEST['textarea'.$a]."', ";

                        }

 

                        if($record['fieldtype']=='upload'){

                        $fieldClause.=$record['fieldname'].", ";

                        $valueClause.="'".$_REQUEST['upload'.$a]."', ";

                        }

                                $a++;

                }

                $fieldClause=substr($fieldClause, 0, strlen($fieldClause)-2);//To remove the trailing ", "

                $valueClause=substr($valueClause, 0, strlen($valueClause)-2);//To remove the trailing ", "

                //echo $fieldClause."<br>";

                                //echo $valueClause."<br>";

                                //$_SESSION['fieldClause']=$fieldClause;

                                //$_SESSION['valueClause']=$valueClause;

                                $sql = "INSERT INTO ".$_SESSION['tname']."(".$fieldClause.") VALUES(".$valueClause.")";

                $qresult= mysql_query($sql) or die("could not insert".mysql_error());

                                ?><table border="1" bgcolor="#FF0000"><tr><td colspan="2">Table name is <?php echo $_SESSION['tname']; ?></td></tr>

                                <tr>

                                <td><?php echo $fieldClause; ?></td><td><?php echo $valueClause; ?></td>

                                </tr>

                                </table><?php 

        }

                //echo $_SESSION['fieldClause']."<br>";

                //echo $_SESSION['valueClause']."<br>";

?>

Open in new window

snap.bmp
0
Comment
Question by:designersx
  • 13
  • 7
20 Comments
 
LVL 39

Expert Comment

by:Roger Baklund
Comment Utility
Try this:
echo '<tr>';

$fieldNames = explode(', ',$fieldClause);

$fieldValues = explode(', ',$valueClause);

while($name = array_shift($fieldNames)) {

  $value = array_shift($fieldValues);

  $value = substr($value,1,strlen($value)-2);

  echo '<td>'.$name.'</td><td>'.$value.'</td>';

}

echo '</tr>';

Open in new window

0
 

Author Comment

by:designersx
Comment Utility
hello cxr sir , nice having u at this time, let me try, wait a minute
0
 
LVL 39

Expert Comment

by:Roger Baklund
Comment Utility
Hello. :)

hm... my code will give all data for one record on a single row, you can move the <tr> so that you get one fieldname/fieldvalue for each row, like this:
$fieldNames = explode(', ',$fieldClause);

$fieldValues = explode(', ',$valueClause);

while($name = array_shift($fieldNames)) {

  $value = array_shift($fieldValues);

  $value = substr($value,1,strlen($value)-2);

  echo '<tr><td>'.$name.'</td><td>'.$value.'</td></tr>';

}

Open in new window

0
 

Author Comment

by:designersx
Comment Utility
sir formatting is still not right.

the output is coming like this. i had 2 fields and only 1 record in the database.

<table border="1" bgcolor="#FF0000">
      <tr><td colspan="2">Table name is app</td></tr>
      <tr><td>id</td><td>1</td><td>location</td><td>chd</td></tr>
</table>


but it should be like this

<table border="1" bgcolor="#FF0000">
      <tr><td colspan="2">Table name is app</td></tr>
      <tr><td>id</td><td>location</td></tr>
      <tr><td>1</td><td>chd</td></tr>
</table>

      
0
 

Author Comment

by:designersx
Comment Utility
let me check ur new code output.
0
 

Author Comment

by:designersx
Comment Utility
as u have said this code shows the latest record that is only 1 record.
can't we do it like showing all the records, only then later on i will be able to edit, add and delete the records.
formatting is not right , please see the snapshot. the correct format i have given in my above post.

all the field names should be in one td and not in rows. every single record should be in one 1 td


snap.bmp
0
 

Author Comment

by:designersx
Comment Utility
fieldClause pertains to field/column names
valueClause pertains to records.

every field names should be in 1 row and various td's , then row closed. now next row started 1st record in 1 row and 2nd record in 2nd row like this.
0
 

Author Comment

by:designersx
Comment Utility
i think for this that i have said above, we need to use 2 while loops.
0
 

Author Comment

by:designersx
Comment Utility
we need to display all the field names first , then display the records below them in rows , rows and rows.
0
 
LVL 39

Expert Comment

by:Roger Baklund
Comment Utility
In the original question you said you wanted this:

<tr>
      <td>id</td><td>7</td>
</tr>
<tr>
      <td>name</td><td>dfgdf</td>
</tr>

...but you have changed your mind, you want all field names in one row, then all values?

Try like below. Lines 3-9 must be in a loop if you want to output multiple records. Can you explain in more detail what the code is supposed to do?
$fieldNames = explode(', ',$fieldClause);

echo '<tr>'.implode('</td><td>',$fieldNames).'</tr>';

$fieldValues = explode(', ',$valueClause);

echo '<tr>';

while($value = array_shift($fieldValues)) {

  $value = substr($value,1,strlen($value)-2);

  echo '<td>'.$value.'</td>';

}

echo '</tr>';

Open in new window

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:designersx
Comment Utility
95% right. 2 issues still

1) <table border="1" bgcolor="#FF0000">
<tr>id</td><td>location</td><td>s1</td><td>d1</tr><tr><td>3</td><td>dfg</td><td>gfh</td><td>bn</td></tr>
no ending of table

1 td is missing after d1, this is the view source.

2) can't we show all the records instead of a single record
0
 
LVL 39

Expert Comment

by:Roger Baklund
Comment Utility
Change line 2 from my latest snippet:

echo '<tr>'.implode('</td><td>',$fieldNames).'</tr>';

...into this:

echo '<tr><td>'.implode('</td><td>',$fieldNames).'</td></tr>';

>> no ending of table

Add this at the end:

echo '</table>';

>> can't we show all the records instead of a single record

The code in your original post seems to be an insert query, there is only one record... if you explain in more detail what the code is supposed to do, I might understand what you mean.
0
 

Author Comment

by:designersx
Comment Utility
thanks for the changes u guided.

actually sir i am dynamically adding the tables , adding the columns , adding the data , from the front end.
the 2 variables $fieldClause contains all the columns  and $valueClause contains all the values.

$sql = "INSERT INTO ".$_SESSION['tname']."(".$fieldClause.") VALUES(".$valueClause.")";
table can be any , so i do not know what can be the fields , so i need to do this .fieldclause contains the fields of any table and then it is inserting the values into the columns.

the below i have given the complete code sir.
<?php

session_start();
 

$host = "localhost";

$username = "root";

$password = "";

$database = "cmsbuilder";
 

$con = mysql_connect($host,$username,$password) or die("could not connect".mysql_error());

$db = mysql_select_db($database,$con);
 
 

$sql="select * from ".$_SESSION['tname'];

$query=mysql_query($sql) or die("could not find the table".mysql_error());

$totalfields=mysql_num_fields($query);

//echo $_SESSION['tname'];

$sql1 = "select * from field_types where tablename='".$_SESSION['tname']."'";

$query1 = mysql_query($sql1) or die("could not execute".mysql_error());
 

//$i=1;

$fieldClause="";

$valueClause="";

for($a=1;$a<=$totalfields;)

        {

                while($record = mysql_fetch_assoc($query1))

                {

                        if($record['fieldtype']=='textbox'){

                        $fieldClause.=$record['fieldname'].", ";

                        $valueClause.="'".$_REQUEST['text'.$a]."', ";

                        }

 

                        if($record['fieldtype']=='wysiwyg'){

                        $fieldClause.=$record['fieldname'].", ";

                        $valueClause.="'".$_REQUEST['textarea'.$a]."', ";

                        }

 

                        if($record['fieldtype']=='upload'){

                        $fieldClause.=$record['fieldname'].", ";

                        $valueClause.="'".$_REQUEST['upload'.$a]."', ";

                        }

				$a++;

                }

                $fieldClause=substr($fieldClause, 0, strlen($fieldClause)-2);//To remove the trailing ", "

                $valueClause=substr($valueClause, 0, strlen($valueClause)-2);//To remove the trailing ", "

                //echo $fieldClause."<br>";

				//echo $valueClause."<br>";

				//$_SESSION['fieldClause']=$fieldClause;

				//$_SESSION['valueClause']=$valueClause;

				$sql = "INSERT INTO ".$_SESSION['tname']."(".$fieldClause.") VALUES(".$valueClause.")";

                $qresult= mysql_query($sql) or die("could not insert".mysql_error());

				

				 

				echo '<table border="1" bgcolor="#FF0000">';

					$fieldNames = explode(', ',$fieldClause);

					echo '<tr><td>'.implode('</td><td>',$fieldNames).'</td></tr>';

					$fieldValues = explode(', ',$valueClause);

					echo '<tr>';

					while($value = array_shift($fieldValues)) {

					  $value = substr($value,1,strlen($value)-2);

					  echo '<td>'.$value.'</td>';

					}

					echo '</tr>';

				echo '</table>';

        }

		//echo $_SESSION['fieldClause']."<br>";

		//echo $_SESSION['valueClause']."<br>";

?>

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
Comment Utility
>> the 2 variables $fieldClause contains all the columns  and $valueClause contains all the values.

Yes, and there is only one record involved. What do you mean when you say you want to display all the records? Do you mean records that was in the table from earlier, in addition to the new record?
0
 

Author Comment

by:designersx
Comment Utility
yes, ,if there are 10 records then show 10 records.
0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 250 total points
Comment Utility
Then you need a select query. Note that this is independant from the posted values and the insert query from your original post. If you execute this after the INSERT, the new record will be included.

Consider using <th> instead of <td> for the column names (line 6).
$res = mysql_query('select * from '.$_SESSION['tname']);

echo '<table border="1" bgcolor="#FF0000">';

$firstrow = true;

while($row = mysql_fetch_assoc($res)) {

  if($firstrow) {

    echo '<tr><td>'.implode('</td><td>',array_keys($row).'</td></tr>';

    $firstrow = false;

  }

  echo '<tr><td>'.implode('</td><td>',array_values($row).'</td></tr>';

}

echo '</table>';

Open in new window

0
 

Author Comment

by:designersx
Comment Utility
error is Parse error: syntax error, unexpected ';' in C:\wamp\www\new\12_table_create\dropdown_values\querytry.php on line 57


<?php

session_start();
 

$host = "localhost";

$username = "root";

$password = "";

$database = "cmsbuilder";
 

$con = mysql_connect($host,$username,$password) or die("could not connect".mysql_error());

$db = mysql_select_db($database,$con);
 
 

$sql="select * from ".$_SESSION['tname'];

$query=mysql_query($sql) or die("could not find the table".mysql_error());

$totalfields=mysql_num_fields($query);

//echo $_SESSION['tname'];

$sql1 = "select * from field_types where tablename='".$_SESSION['tname']."'";

$query1 = mysql_query($sql1) or die("could not execute".mysql_error());
 

//$i=1;

$fieldClause="";

$valueClause="";

for($a=1;$a<=$totalfields;)

        {

                while($record = mysql_fetch_assoc($query1))

                {

                        if($record['fieldtype']=='textbox'){

                        $fieldClause.=$record['fieldname'].", ";

                        $valueClause.="'".$_REQUEST['text'.$a]."', ";

                        }

 

                        if($record['fieldtype']=='wysiwyg'){

                        $fieldClause.=$record['fieldname'].", ";

                        $valueClause.="'".$_REQUEST['textarea'.$a]."', ";

                        }

 

                        if($record['fieldtype']=='upload'){

                        $fieldClause.=$record['fieldname'].", ";

                        $valueClause.="'".$_REQUEST['upload'.$a]."', ";

                        }

				$a++;

                }

                $fieldClause=substr($fieldClause, 0, strlen($fieldClause)-2);//To remove the trailing ", "

                $valueClause=substr($valueClause, 0, strlen($valueClause)-2);//To remove the trailing ", "

                //echo $fieldClause."<br>";

				//echo $valueClause."<br>";

				//$_SESSION['fieldClause']=$fieldClause;

				//$_SESSION['valueClause']=$valueClause;

				$sql = "INSERT INTO ".$_SESSION['tname']."(".$fieldClause.") VALUES(".$valueClause.")";

                $qresult= mysql_query($sql) or die("could not insert".mysql_error());

				

				$res = mysql_query('select * from '.$_SESSION['tname']);

				echo '<table border="1" bgcolor="#FF0000">';

				$firstrow = true;

				while($row = mysql_fetch_assoc($res)) {

				  if($firstrow) {

					echo '<tr><td>'.implode('</td><td>',array_keys($row).'</td></tr>';

					$firstrow = false;

				  }

				  echo '<tr><td>'.implode('</td><td>',array_values($row).'</td></tr>';

				}

				echo '</table>';

        }

		//echo $_SESSION['fieldClause']."<br>";

		//echo $_SESSION['valueClause']."<br>";

?>

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
Comment Utility
Sorry, it is missing a ) on that line, and also on line 60. Line 57:

echo '<tr><td>'.implode('</td><td>',array_keys($row)).'</td></tr>';

Line 60:

echo '<tr><td>'.implode('</td><td>',array_values($row)).'</td></tr>';
0
 

Author Comment

by:designersx
Comment Utility
it is perfect sir.
0
 

Author Comment

by:designersx
Comment Utility
thnks sir
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now