Solved

what to use explode or implode after  48 line

Posted on 2009-06-26
20
204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 7
20 Comments
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24726909
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
ID: 24726974
hello cxr sir , nice having u at this time, let me try, wait a minute
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24727005
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:designersx
ID: 24727030
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
ID: 24727035
let me check ur new code output.
0
 

Author Comment

by:designersx
ID: 24727048
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
ID: 24727053
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
ID: 24727056
i think for this that i have said above, we need to use 2 while loops.
0
 

Author Comment

by:designersx
ID: 24727059
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
ID: 24727192
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
 

Author Comment

by:designersx
ID: 24727382
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
ID: 24727399
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
ID: 24727433
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
ID: 24727447
>> 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
ID: 24727473
yes, ,if there are 10 records then show 10 records.
0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 250 total points
ID: 24727704
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
ID: 24727761
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
ID: 24727903
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
ID: 24727921
it is perfect sir.
0
 

Author Comment

by:designersx
ID: 24739089
thnks sir
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

756 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