what to use explode or implode after 48 line

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
designersxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Roger BaklundCommented:
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
designersxAuthor Commented:
hello cxr sir , nice having u at this time, let me try, wait a minute
0
Roger BaklundCommented:
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
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

designersxAuthor Commented:
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
designersxAuthor Commented:
let me check ur new code output.
0
designersxAuthor Commented:
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
designersxAuthor Commented:
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
designersxAuthor Commented:
i think for this that i have said above, we need to use 2 while loops.
0
designersxAuthor Commented:
we need to display all the field names first , then display the records below them in rows , rows and rows.
0
Roger BaklundCommented:
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
designersxAuthor Commented:
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
Roger BaklundCommented:
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
designersxAuthor Commented:
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
Roger BaklundCommented:
>> 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
designersxAuthor Commented:
yes, ,if there are 10 records then show 10 records.
0
Roger BaklundCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
designersxAuthor Commented:
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
Roger BaklundCommented:
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
designersxAuthor Commented:
it is perfect sir.
0
designersxAuthor Commented:
thnks sir
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.