Solved

what to use explode or implode after  48 line

Posted on 2009-06-26
20
202 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
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Checking CSRF token within a function 36 76
Google Maps API and PHP 25 33
PHP not parsing ' character 12 40
how to access a remote mysql database with xampp 3 9
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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 dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

809 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