Re: one click - two actions?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



At 11/13/2006 01:28 AM, Mel wrote:
Could someone please help me figure out how to show some description
(where applicable) at the same time as I show an image, when I click
on a link, without repeating the entire query?
The image and the description are both in the same table in my database.

I now show the image when I click on the link which is good, but the
description stays on at all times instead of appearing only when active.

http://www.squareinch.net/single_page.php


Mel,

I think what you're looking for is JOIN syntax for your queries:
http://dev.mysql.com/doc/refman/4.1/en/join.html

For example:

        SELECT * FROM client, job
        WHERE client.companyId=job.companyId
        AND (client.status='active' or client.status='old')
        order by client.companyName;

(Note that when you extract fields from more than one table like this, you identify the table that each field belongs to, e.g. client.companyId.)

Then you can extract the desired fields from both tables in the same loop because they've been captured together. Your current logic executes a job query for every row of client, which is extremely inefficient.

The dataset produced by the join query is going to look something like this:

client.         job.
companyId       companyId
    1           2
    1           3
    1           9
    2           4
    2           5
...

In other words, there will be one row for each job record, with the (parent) client fields duplicated each row.


You can further improve the efficiency of your query by naming only the fields you need, instead of using * to extract all fields:

        SELECT client.companyName, job.pix, job.jobType, job.url, job.web
        FROM client, job
        WHERE client.companyId=job.companyId
        AND (client.status='active' or client.status='old')
        order by client.companyName;

Once you execute the join query, your PHP loop can cycle in a similar way, echoing a company name and then listing all the job types until a new company name occurs, etc.


You've got other problems, however. If you look at your HTML source, you'll see markup like this:

<span class='navCompany'>Builtworks</span><span class='navArrow'>  > </span>
<span class='navText'><a href='single_page.php?art=btw_logo.jpg'>logo</a></span>
<span class='navText'><a href='single_page.php?art='></a></span>
<span class='navText'><a href='single_page.php?art='></a></span>
<span class='navText'><a href='single_page.php?art='></a></span>
<span class='navText'><a href='single_page.php?art='></a></span>
<br><span class='navCompany'>Citizens Bank / eProperty</span><span class='navArrow'> > </span> <span class='navText'><a href='single_page.php?art=ctz_web1.jpg'>website</a></span>

All those empty hyperlinks aren't doing anything but making your download heavier than it has to be. I think you need to test your 'jobType' fields and output only those that aren't blank.


Finally, to answer one of your questions, your logic to display the description area has a snarl of syntax flaws:

/* query 2 from job */
...
        foreach($row as $url)
                {
                $row = mysql_fetch_array($result2,MYSQL_ASSOC);
                if ("url={$row['url']}")
echo "<span class='navText'><a href='{$row['url']}'>{$row['web']}</ a></span>";
                }

You're testing if ("url={$row['url']}")

1) Because you've put that expression in quotes, you're testing the true/false value of a string expression which will always test true unless it's blank, which this one will never be.

Expressing it as a string might be correct if you were using eval(), but you're not and you're safer not to. Eval() can get you into big trouble if there are PHP code fragments in your database fields; until you get better control of your logic I urge you not to use it.

2) You omitted the $ in front of $url.

3) You used a single equal sign instead of two.  This:
        if ($url = $row['url'])
tests whether $row['url'] is blank, and also sets $url equal to that value.

I think you meant this:
        if ($url == $row['url'])
which tests whether the variable $url is equal to the database field $row['url'].


Good luck,
Paul



This is the code I have for the image area:
/* query 1 from client */
  $query = "SELECT * FROM client
                                where status='active' or status='old'
                                order by companyName";

  $result = mysql_query($query)
                                or die ("Couldn't execute query");

  while         ($aaa = mysql_fetch_array($result,MYSQL_ASSOC))
  {
  echo "<span class='navCompany'>{$aaa['companyName']}</span><span
class='navArrow'>  > </span>\n";

        /* query 2 from job */
        $query = "SELECT * FROM job
                                WHERE companyId='{$aaa['companyId']}'";
        $result2 = mysql_query($query)
                or die ("Couldn't execute query2");

                        foreach($aaa as $jobType)
                        {
                        $bbb = mysql_fetch_array($result2,MYSQL_ASSOC);
echo "<span class='navText'><a href='single_page.php?art=".$bbb ['pix']."'>{$bbb['jobType']}</a></span>\n";
                        }
        echo "<br>";
        }
        ?>

</div>


<div class="navbox3"><?php $image = $_GET['art']; ?>
<img src="images/<?php print ($image) ?>" alt="Portfolio Item"
border="0" width="285" height="285"></div>


This is the code I have for the description area:

/* query 1 from client */
$query = "SELECT * FROM client
                        where status='active' or status='old'
                        order by companyName";

        $result = mysql_query($query)
        or die ("Couldn't execute query");

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

/* query 2 from job */
$query = "SELECT * FROM job
                                WHERE companyId='{$row['companyId']}'";
                $result2 = mysql_query($query)
                or die ("Couldn't execute query2");
                $url = mysql_query($result2);

        foreach($row as $url)
                {
                $row = mysql_fetch_array($result2,MYSQL_ASSOC);
                if ("url={$row['url']}")
echo "<span class='navText'><a href='{$row['url']}'>{$row['web']}</ a></span>";
                }

        echo "<br>";
        }
        ?>



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux