...

View Full Version : Array to Table: Help me clean my code



Sapient
07-02-2007, 05:25 PM
I am a coding novice who has assembled the following code from various sources. I am taking long strings of text from a single field in an Oracle database. This text is tab delimited data of varying length. Rather than using

list($a, $b, $c, $d, $e, $f, $g, $h, $j, $k, $l, $m, $n, $o, $p, $q, $r, $t, $u, $v, $w, $x, $y, $z) = split("\t", oci_result($s,$i), 24);
print "<td>$a</td><td>$b</td><td>$c</td><td>$d</td><td>$e</td>...[SHORTENED FOR EASIER FORUM READING].......<td>$z</td>";

to get this data into cells in a table, I would like to clean up the code with something like

$data = split("/t", $s);

so that there are no more fields created than needed.

Unfortunately, my coding skills are weak enough that I can't figure out how to print this into a table correctly. Here is my full code. Can anyone help me? (And if someone wants to help me print the rows of my table in alternating colors, I would appreciate that too). Thanks much!


<html>
<body>

<?php

PutEnv("ORACLE_SID=PROD");
PutEnv("ORACLE_HOME=/usr/local/oracle10g/product/10.2.0/db_1");
PutEnv("LD_LIBRARY_PATH=/usr/local/oracle/oracle10gR2_client/oracle/lib");
$dataset = $_GET["dataset"];
#$dataset = 'gm85e09';

$c=OCILogon("xxxxx", "xxxxx", "xxxxx");
$s = OCIParse($c, "select dataset_value from dataset_data where dataset_name = '$dataset'");
OCIExecute($s, OCI_DEFAULT);

print "Data from <B>$dataset </B>";
print '<table border="1" cellspacing="0" cellpadding="3">';


while (oci_fetch($s))
{
// Print open and close HTML row tags and columns data.
print '<tr>';
for ($i = 1;$i <= oci_num_fields($s);$i++){
#print '<td class="v">'.oci_result($s,$i).'</td>';
list($a, $b, $c, $d, $e, $f, $g, $h, $j, $k, $l, $m, $n, $o, $p, $q, $r, $t, $u, $v, $w, $x, $y, $z) = split("\t", oci_result($s,$i), 24);
print "<td>$a</td><td>$b</td><td>$c</td><td>$d</td><td>$e</td>...[SHORTENED FOR EASIER FORUM READING]....<td>$z</td>";
}
print '</tr>';
}

print "</table>";


?>

</body></html>

aedrin
07-02-2007, 06:22 PM
I won't comment on your use of tab delimiting in a database (especially Oracle ;)).



$data = split("/t", $s);

foreach ($data as $value) {
echo '<td>'.$value.'</td>';
}

Sapient
07-02-2007, 06:49 PM
Thanks. I must be doing something wrong, because instead of getting rows with many fields, I get rows with one field, all of which say "Resource id #3". Here is my modified code. Can you tell what I am doing wrong?


<html>
<body>

<?php

PutEnv("ORACLE_SID=PROD");
PutEnv("ORACLE_HOME=/usr/local/oracle10g/product/10.2.0/db_1");
PutEnv("LD_LIBRARY_PATH=/usr/local/oracle/oracle10gR2_client/oracle/lib");
$dataset = $_GET["dataset"];
#$dataset = 'gm85e09';

$c=OCILogon("xxxxx", "xxxxx", "xxxxx");
$s = OCIParse($c, "select dataset_value from dataset_data where dataset_name = '$dataset'");
OCIExecute($s, OCI_DEFAULT);

print "Data from <B>$dataset </B>";
print '<table border="1" cellspacing="0" cellpadding="3">';


while (oci_fetch($s))
{
// Print open and close HTML row tags and columns data.
print '<tr>';
for ($i = 1;$i <= oci_num_fields($s);$i++){

#print '<td class="v">'.oci_result($s,$i).'</td>';
$data = split("/t", $s);

foreach ($data as $value) {
echo '<td>'.$value.'</td>';
}
}
print '</tr>';
}

print "</table>";


?>

</body></html>

Yeah, I know my Oracle strategy stinks. Unfortunately, I have about a thousand datasets, all with different formats and sizes, that need to be consolidated quickly into Oracle and gotten online. They are all going into a single table with fields identifying what data is from what dataset.

aedrin
07-02-2007, 09:46 PM
You probably need to change the first line of my example to this:



$data = split("\t", oci_result($s,$i);


I was assuming $s was the actual string to be seperated.

Sapient
07-02-2007, 09:54 PM
You probably need to change the first line of my example to this:



$data = split("\t", oci_result($s,$i);


I was assuming $s was the actual string to be seperated.

I've done that. My output right now places all values into a single field so that my rows are turned vertically and stacked on top of each other. I'll keep trying.

I do appreciate the help, though. Any ideas are welcome.

Sapient
07-02-2007, 10:15 PM
Never mind. I was messing up my for loop. Thanks for your help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum