Page 1 of 1

Help with code.

Posted: Sat Dec 28, 2002 8:50 pm
by eggoz
I am a beginning with php, I have a book and everything, but I had a question about nesting html code. So far I have a table that calls data from my database, but I can not figure out to create links on one of the columns. Here is what I have:

Code: Select all

print "<table border=1>\n";
while ( $b_row = mysql_fetch_row( $results ) ) &#123;
	print "<tr>\n";
	foreach ( $b_row as $field )
		print "\t<td>;
		print "<a href="$field">$field</a>;
		</td>\n";
	print "</tr>\n";
&#125;
print "</table>\n";
I'm sure by the code you can tell what I am trying to do. Yet, it doesn't work. Where am I going wrong on this? Thanks.

Posted: Sat Dec 28, 2002 9:41 pm
by evilcoder
have you defined the $field variable?

Yes.

Posted: Sat Dec 28, 2002 9:53 pm
by eggoz
Yes, here is my original code. Till this point, everything worked.

Code: Select all

print "<table border=1>\n";
while ( $a_row = mysql_fetch_row( $result ) ) &#123;
	print "<tr>\n";
	foreach ( $a_row as $field )
		print "\t<td>$field</td>\n";
	print "</tr>\n";
&#125;
print "</table>\n";

Posted: Sun Dec 29, 2002 10:32 am
by Gen-ik
I think you missed out something around the $field variable.. try this..

print "<table border=1>\n";
while ( $b_row = mysql_fetch_row( $results ) )
{
print "<tr>\n";
foreach ( $b_row as $field )
print "\t<td>";
echo ("<a href=\"".$field."\">".$field."</a>\n");
print "</td>\n";
print "</tr>\n";
}
print "</table>\n";

Posted: Sun Dec 29, 2002 11:14 am
by gaagaagui
Instead of this:
print "<table border=1>\n";
while ( $b_row = mysql_fetch_row( $results ) ) {
print "<tr>\n";
foreach ( $b_row as $field )
print "\t<td>;
print "<a href="$field">$field</a>;
</td>\n";
print "</tr>\n";
}
print "</table>\n";

how about this:
<table border=1>
<?
$nr = mysql_num_rows($results);
for ($a=0; $a<$nr; $a++) {
$b_row = mysql_fetch_row( $results );
?>
<Tr>
<?
foreach ($b_row as $field) {
?>
<a href="<? echo $field; ?>"><? echo $field; ?></a>
<?
}
?>
</tr>
<?
}
?>
</table>
Number 1, notice that you need a begin/end bracket to to properly display the table row after the foreach line. I have my code heavily formatted and I replaced your while loop with a for loop. I also get the number of rows in the result set before that for loop starts. You may look at this code as unnessessarily clogged with formatting, but you can see that when coding, if you are indenting properly and formatting begin/end brackets that I do, you can trouble shoot most errors before even trying to process your scripts. The previous replier didn't even catch the missing brackets because he/she probobly formats his php code like you do (or did hopefully) and could not see the obvious. Try my suggestions.

Thanks, but...

Posted: Sun Dec 29, 2002 7:32 pm
by eggoz
Gen-ik: This code worked for me, except for 2 problems. The first column doesn't show up at all. And second (this not being your fault) I don't think this is going to accomplish what I am trying to do. My idea was the link that I created would open up a new page. This new page would load the values from the record corresponding to the link. Did I loose you? My idea was to create something like this page here http://www.computersurplusoutlet.com/sh ... ategory=21. When you click on one of the products, all its info would pop up. And all this information would be read from the fields of the one record. Any ideas on how this can be done? I don't mean script wise, just speaking in terms of design. Thanks.

progress

Posted: Sun Dec 29, 2002 8:09 pm
by eggoz
gaagaagui: at first i hated your script. but the more i played with it, the more i enjoyed it. but once again, I can not figure out how to only get one column to be a "link". I get all columns to become links. Back to work...

Posted: Mon Dec 30, 2002 12:00 am
by gaagaagui
Questions:
1. Do you only need one field from each row of your result set to display as a hyperlink?
2. If yes, do you need to display the other fields from each row of your result set?
3. If no, do you know the name of the field from the table you are reading from?

If you have answered yes to this point, I would guess you want to do the following:

1. do a select query from a table where all you need is the value of one field
2. create an html table, loop through your result set, diplaying, for each iteration of your loop, an html table row with one table cell (<td></td>) containing a hyperlink like the following:
<a href="<? echo $field; ?>"><? echo $field; ?></a>
Yes? If yes, then you can use $b_row to access the field you want...
If the fieldname was URL, you would use $b_row->URL to reference the field and my example above:
<a href="<? echo $field; ?>"><? echo $field; ?></a>
would become:
<a href="<? echo $b_row->URL; ?>"><? echo $b_row->URL; ?></a>

Assuming this is correct, then if you modify my previous posting of code, you would have:
<table border=1>
<?
$nr = mysql_num_rows($results);
for ($a=0; $a<$nr; $a++) {
$b_row = mysql_fetch_row( $results );
?>
<Tr>
<a href="<? echo $b_row->URL; ?>"><? echo $b_row->URL; ?></a>
<?
}
?>
</tr>
<?
}
?>
</table>
Am I on the right track or lost in space?

Space...

Posted: Mon Dec 30, 2002 12:29 am
by eggoz
Not excatly what I had in mind. I figured it would be easier if I just showed you an example using plain HTML. http://diegostatic.dyndns.org:88/example.htm I have been trying to do it, but so far no such luck. I am trying to do two queries, each for one column. But I haven't figured out how to create just one table. Thanks.[/url]

Posted: Mon Dec 30, 2002 1:07 am
by gaagaagui
Alright, my curiosity is getting the best of me...
When you say you are trying to do two separate queries, do you mean 2 separate select queries of one table or 2 separate queries each to a different table?
If two queries of one table can it be assumed that the number of rows in the result set will be the same for both queries.
or
if two queries from 2 tables can it be assumed that the number of rows in the result set will be the same for both queries.

If no for either, I think you're <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>...
If yes for either, just get the number of rows for either result set, use a for loop to loop that many times, and read a record from one result set for the hyperlink in column A and read a record from the other result set for the value in column B
What say you...

I've improved

Posted: Mon Dec 30, 2002 1:41 am
by eggoz
Take a look. Thanks to all your help, this book, and no sleep, i've gotten quite a lot done. I was able to create the table, one column is now a link to a page called details, and I passed the values from the second column. http://www.aardvarksafety.com/try6.php Now I need a suggestion. What is the simpliest way to pass all the fields in one record to the details page? I was thinking about hiding the values, but my idea is to pass descriptions of products to the details page. This would be too much text to pass from one page to another, or am I wrong? I look forward to hearing ideas to my problem. Thanks again.

Before I go to bed...

Posted: Mon Dec 30, 2002 1:56 am
by eggoz
Before I go to bed, I tried this last attempt. But didn't work. Any ideas why?

Code: Select all

$user = "root";
$pass = "*******";
$db = "test";

$link = mysql_connect("localhost",$user, $pass );
if (! $link )
	die("couldn't connect to server" );
mysql_select_db ($db, $link )
	or die ( "could not open $db ".mysql_error() );
$record = mysql_query ( SELECT * FROM testtable WHERE name = "$value" );
?>
print "<table border=1>\n"; 
while ( $a_row = mysql_fetch_row( $record ) ) &#123; 
   print "<tr>\n"; 
   foreach ( $a_row as $field ) 
      print "\t<td>$field</td>\n"; 
   print "</tr>\n"; 
&#125; 
print "</table>\n";
mysql_close( $link );


<?php echo $value; ?>
Almost got this...