Jump to content

aaa.... help?


Valentina

Recommended Posts

I will start by saying that I am a beginner in PHP, and when I say beginner I mean "dummy" actually.

I read a tutorial online and now I have dreams of writing php scripts....

Anyway, I am trying to create a search function for a database, using MySql. I managed to do that but in the table that displays the results I want to have the items in the first column linked to a new page  that opens on click (to show more details about the respective item, in the new page). My problem is that I don't know how to import the MySql array into the new page created.

Could somebody help me, please?

Thank you very much!

 

 

 

Valentina

Link to comment
Share on other sites

<?php
//.... normal connecting etc crap goes here

$res = mysql_query("select * from TABLE where CONDITIONS");
echo "<table border=0 cellpadding=2 cellspacing=0><tr><td colspan=~N>CAT NAME</td></tr>
";
for($i=0;$i<mysql_num_rows($res)) {
   $arr = mysql_fetch_array($res);
   echo "<tr><td><a href=PAGE.php?CONDITION=$arr[CON]>$arr[CON2]</a></td><td>ETC....</td></tr>
";
}
echo "</table>";
//.... etc
?>

 

and on page 2...

 

<?php
//.....()
$res = mysql_query("select * from TABLE where CONDITION='$_GET[CONDITION]'");
$arr = mysql_fetch_array($res);
/* handle the data and display how you want it displayed */
/.....etc
?>

 

 

hope this helped

 

-T`L

Link to comment
Share on other sites

<?php
//.... normal connecting etc crap goes here

$res = mysql_query("select * from TABLE where CONDITIONS");
echo "<table border=0 cellpadding=2 cellspacing=0><tr><td colspan=~N>CAT NAME</td></tr>
";
for($i=0;$i<mysql_num_rows($res)) {
   $arr = mysql_fetch_array($res);
   echo "<tr><td><a href=PAGE.php?CONDITION=$arr[CON]>$arr[CON2]</a></td><td>ETC....</td></tr>
";
}
echo "</table>";
//.... etc
?>

 

and on page 2...

 

<?php
//.....()
$res = mysql_query("select * from TABLE where CONDITION='$_GET[CONDITION]'");
$arr = mysql_fetch_array($res);
/* handle the data and display how you want it displayed */
/.....etc
?>

 

 

hope this helped

 

-T`L

 

I'm unsure if that works. Also, the $i variable never gets incremented.

<?php
//.... normal connecting etc crap goes here

$res = mysql_query("select * from TABLE where CONDITIONS");
echo "<table border=0 cellpadding=2 cellspacing=0><tr><td colspan=~N>CAT NAME</td></tr>
";
while ($row = mysql_fetch_assoc($res)) {
   echo "<tr><td><a href=PAGE.php?CONDITION={$row['CON']}>{$row['CON2']}</a></td><td>ETC....</td></tr>";
}
echo "</table>";
//.... etc
?>

 

<?php
//.....()
$res = mysql_query("select * from TABLE where CONDITION='{$_GET['CONDITION']}'");
$arr = mysql_fetch_array($res);
/* handle the data and display how you want it displayed */
/.....etc
?>

Link to comment
Share on other sites

Well... I guess I need more brain  :(

I tried the code and I get an error message... the "CONDITIONS" are not imported in the new file.

Everything works great until it generates the new file...

I am sure I am doing something wrong... Could you please take another look at this?

Here's the page where I am trying this:

http://www.valentinac.com/test stuff/search5.php

You can only search for "America", "Lipstick", "Ecuador Pink"... it's just a small database for trying the script.

 

And this is my code:

 

<?php
mysql_connect(...etc...) or die(mysql_error());
mysql_select_db("...database...") or die(mysql_error());
$query = "SELECT * FROM Brugmansia WHERE Cultivar LIKE \"%$cleanx%\" OR PodParent LIKE \"%$cleanx%\" or PollenParent LIKE \"%$cleanx%\" ORDER by Cultivar"; 
$result = mysql_query($query) or die("Couldn't execute query");

echo "<table border=\"1\">"; 
echo "<tr><th>Cultivar</th>"; 
echo "<th>Pod Parent</th>";
echo "<th>Pollen Parent</th>";
...etc

  while ($row= mysql_fetch_array($result)) { 


  $myFile = "newfile.php";
  $fh = fopen($myFile, 'w') or die("can't open file");
  $stringData = <<<ABC

<html>
<head>
<title>
</title>
</head>

<body>

<?php
//.....()
$result = mysql_query("SELECT * FROM Brugmansia where CONDITION='{$_GET['CONDITION']}'");
$row = mysql_fetch_array($result)

echo "<p>This is a page for " .  $row['Cultivar'] . "<br>"; 
}
?>


<p>something else here...</p>
</body>
</html>
ABC;
fwrite($fh, $stringData);
fclose($fh);


                          echo "<tr>"; 
	 echo "<td>"; 
	 echo "<b><a href=\"newfile.php?CONDITION={$row['Cultivar']}\" target=\"_blank\">" . $row['Cultivar'] . "</a>" . "</b></td>"; 
     echo "<td>" . $row['PodParent'] . "</td>"; 
     echo "<td>" . $row['PollenParent'] . "</td>"; 
                 ....etc.....
     echo "</tr>";
  }
echo "</table>";	
?>

 

All this works only if I don't generate the new file.

And even when I do, it works as long as I don't use the databse variables in the code.

What am I doing wrong?

Thank you for your help!

 

 

 

Valentina

Link to comment
Share on other sites

I don't understand why you are using fopen in the file that you have open. And you are using 'w' for the mode, which means it truncates the file length to zero.

 

Oh wait, that's the search page. Did you have a look at the code posted by True Logic.

 

Instead of writing the data to newfile.php you simply have some more code in it. This code grabs the condition out of the address bar and queries it into the database to select the relevant information.

 

Make sense?

Link to comment
Share on other sites

I don't understand why you are using fopen in the file that you have open. And you are using 'w' for the mode, which means it truncates the file length to zero.

 

I am trying to generate a new file (newfile.php), from the search file, that will show more details about a certain row in the database. And I need this file to be generated new, clean, for every item/row. If you go to the link I gave earlier you will have a better idea about what I am trying to do...

 

Did you have a look at the code posted by True Logic.

 

Yes. I changed the while loop into a for loop and it works, but I get exactly the same results (as for the while loop). The loop is not the problem. I get the error when I try to implement the Mysql results into the newfile.php.

 

Thank you for your help!

 

 

 

Valentina

Link to comment
Share on other sites

I just want to say "Thank you" again, for all your help.

My script works now... I finally discovered what I was doing wrong.

Probably most of you already know this, but maybe it's useful for beginners like me, so I will explain:

I was using the "heredoc syntax" to write in my newfile.php and I didn't know that this syntax is escaping the "$" sign. The tutorial I read (where I found out about this syntax) did not give me this information :) I finally got smart and opened the newfile.php to see what am I writing in there...

 

Thank you, everybody! I appreciate it.

 

 

 

Valentina

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.