/    Sign up×
Community /Pin to ProfileBookmark

display hyperlink once for several contents

Hi,
I have the following php code to get the hyperlink of the retrieved company name(s), which may related to several entries to be displayed when the hyperlink is clicked:

[code=php]
<?php

$dbcnx = @mysql_connect(‘localhost’, ‘root’, ‘password’);
if (!$dbcnx) {
exit(‘<p>Unable to connect to the database server at this time.</p>’);
}

if (!@mysql_select_db(‘ijob’)) {
exit(‘<p>Unable to locate the blog collection database at this time.</p>’);
}

?>
<?php

$id = $_GET[‘id’];
$select = “SELECT comp_id, comp_name from company_list where comp_id=’$id’ order by comp_name”;

$retrieve = @mysql_query($select);
if(!$retrieve) {
exit (‘<p>Error retrieving blog entries from database!<br />’.
‘Error: ‘.mysql_error(). ‘</p>’);
}
while ($result = mysql_fetch_array($retrieve)) {
if ($result != “”) {
$a_name = $result[‘comp_name’];

$a_qeury = “select id, title, content from blog_entry where content like “;
$a_qeury = $a_qeury . ‘”%’;
$a_qeury = $a_qeury . $a_name;
$a_qeury = $a_qeury . ‘%”‘;
$a_result = @mysql_query($a_qeury);
if(!$a_result) {
exit (‘<p>Error retrieving blog entries from database!<br />’.
‘Error: ‘.mysql_error(). ‘</p>’);
}
while ($a_output = mysql_fetch_array($a_result)){

if ($a_output != “”) {
echo ‘<a href=”blogentry_name.php?id=’.$a_output[‘id’].'”>’.$result[‘comp_name’].'</a>’;
}

}
}
}
?>
[/code]

blogentry_name.php:

[code=php]
<?php

$dbcnx = @mysql_connect(‘localhost’, ‘root’, ‘password’);
if (!$dbcnx) {
exit(‘<p>Unable to connect to the database server at this time.</p>’);
}

if (!@mysql_select_db(‘ijob’)) {
exit(‘<p>Unable to locate the blog collection database at this time.</p>’);
}

?>
<?php

$id = $_GET[‘id’];
$select = “SELECT DISTINCT title, id, publish_date from blog_entry where id=’$id’ GROUP BY title order by publish_date”;

$retrieve = @mysql_query($select);
if(!$retrieve) {
exit (‘<p>Error retrieving blog entries from database!<br />’.
‘Error: ‘.mysql_error(). ‘</p>’);
}
while ($result = mysql_fetch_array($retrieve)) {
if ($result != “”) {
echo ‘<a href=”comp_content.php?id=’.$id.'”>’.$result[‘title’].'</a>’;
echo “$result[publish_date]”;
}
}
?>
[/code]

As a result, the same company name will appear multiple time when it related to multiple number of entries. For example, there are 3 entries related to “AOL” and “AOL” will appear 3 times, each links to different entry. :rolleyes: However, this is not as what I want. It is designed to display the same comapnay name once; when this company name is clicked, the list of related entries will appear. So how should I change the codes ? ?

Thank you very much. ?

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@DJsACSep 04.2006 — [code=php]
//define:
$company_array = array();
//somewhere above this...

while ($a_output = mysql_fetch_array($a_result)){

if ($a_output != "") {
if(!in_array($result['comp_name'],$company_array))
{
$company_array[] = $result['comp_name']; //add company to array
echo '<a href="blogentry_name.php?id='.$a_output['id'].'">'.$result['comp_name'].'</a>'; // echo link for company.
}
else
{
//the company has already got a link.
}
}


}[/code]


In case you wanted the 1 link to contain all the id's just yell, and I'll fix it. ?
Copy linkTweet thisAlerts:
@yunfannyauthorSep 05.2006 — Hi, DJsAC,

Actually there is one more page before I display the company name--The initial of the company names are displayed 1st. For example, "A" is displayed once and linked to the list of companies naming startin with "A", then each company linkes to a list of related entries.

The following is the PHP code of displaying the initial of the company, which isedited according to your previous coding. However, "A" still appears 3 times when there are 3 companies starting wth "A".
[code=php]
<?php

$dbcnx = @mysql_connect('localhost', 'root', 'password');
if (!$dbcnx) {
exit('<p>Unable to connect to the database server at this time.</p>');
}

// Select the blog database
if (!@mysql_select_db('ijob')) {
exit('<p>Unable to locate the blog collection database at this time.</p>');
}
?>

<?php
$a_name_query = "select comp_id, comp_name from company_list where comp_name like 'A%'";
$a_name_result = @mysql_query($a_name_query);
if(!$a_name_result) {
exit ('<p>Error retrieving company names from database!<br />'.
'Error: '.mysql_error(). '</p>');
}

$A_array = array();

while ($a_name_output =mysql_fetch_array($a_name_result)){
$a_name = $a_name_output['comp_name'];
$a_name_id = $a_name_output['comp_id'];

$a_qeury = "select content from blog_entry where content like ";
$a_qeury = $a_qeury . '"%';
$a_qeury = $a_qeury .' '. $a_name.' ';
$a_qeury = $a_qeury . '%"';
$a_result = @mysql_query($a_qeury);
if(!$a_result) {
exit ('<p>Error retrieving blog entries from database!<br />'.
'Error: '.mysql_error(). '</p>');
}
$a_output = mysql_fetch_array($a_result);
if ($a_output != "") {

if ($a_output != "") {
if(!in_array($a_name_output['comp_name'],$A_array))
{
$A_array[] = $a_name_output['comp_name']; //add company to array
echo '<a href="comp_name.php?id='.$a_name_output['comp_id'].'">A</a>';
}



}

}
}
?>
[/code]


HELP ! ?
Copy linkTweet thisAlerts:
@DJsACSep 05.2006 — Not sure If I understood you completely...

You want to first make a link:

A | B | C | D | F | G | etc... if they click that, they get a list of companies with that letter. In this example there are no companies with E so it does not get listed.

Upon clicking the letter, the user gets a list of all the companies that start with that letter. Company 'AOL' might be in the list 20 times, so limit the script to only give 1 link for AOL. Clicking on AOL will give you a page with data about the 20 AOL entries....

I editted your script from your latest post,

and changed it to a function. This way you won't have to repeat it for every letter (which I expect you were doing...)

It also supports both the 'A' Link as wel as the 'AOL' link... ?

if this is NOT what you meant, It should still help you in the right direction.

If what I'm saying is a load of crap, just ignore me :p

Here's the code:
[code=php]
<?php

$dbcnx = @mysql_connect('localhost', 'root', 'password');
if (!$dbcnx) {
exit('<p>Unable to connect to the database server at this time.</p>');
}

// Select the blog database
if (!@mysql_select_db('ijob')) {
exit('<p>Unable to locate the blog collection database at this time.</p>');
}
?>

<?php
function get_list($letter="A",$output=1)
{
$valid_letter = false;
$letter_name_query = "select comp_id, comp_name from company_list where comp_name like '".$letter."%'";
$letter_name_result = @mysql_query($letter_name_query);
$letter_name_count = mysql_num_rows($letter_name_result);
if($letter_name_count > 0)
{
$valid_letter = true;
}
if(!$letter_name_result)
{
exit ('<p>Error retrieving company names from database!<br />Error: '.mysql_error(). '</p>');
}

if($output == 1 && $valid_letter === true)
{
echo '<a href="comp_name.php?id='.$letter.'">'.$letter.'</a>';
}
elseif($output==1 && $valid_letter === false)
{
//echo "There are no companies available with the letter '".$letter."'.";//uncomment to show errormsg.
}
elseif($output == 2)
{
$letter_array = array();
while ($letter_name_output =mysql_fetch_array($letter_name_result))
{
$letter_name = $letter_name_output['comp_name'];
$letter_name_id = $letter_name_output['comp_id'];

$letter_query = "select content from blog_entry where content like ";
$letter_query .= '"%'. $letter_name.'%"';
$letter_result = @mysql_query($letter_query);
if(!$letter_result)
{
exit ('<p>Error retrieving blog entries from database!<br />'.
'Error: '.mysql_error(). '</p>');
}
$letter_output = mysql_fetch_array($letter_result);
if ($letter_output != "")
{
if(!in_array($letter_name,$letter_array))
{
$letter_array[] = $letter_name; //add company to array
echo '<a href="comp_name.php?id='.$letter_name.'">'.$letter_name.'</a>';
}
}
}//end while
}//end else
else
{
return NULL;
}
}//end function

//to output link to company page A
get_list("A",1);
//to output links to all company's with A
get_list("A",2);
//to output link to company page F
get_list("F",1);
//to output links to all company's with F
get_list("F",2);
?> [/code]
Copy linkTweet thisAlerts:
@yunfannyauthorSep 07.2006 — Hi, DJsAC,

Thank you a lot for your time.

Well, you got most of my ideas. Actually I will only need to display the initials of the companies after I confirm there are entries containing the corresponding company names. In the database, the 26 letters all appear as the intial in some names, so there is no need to check whether there is such company names with certain initials. In another word, I will display "A" if the entries containing company names like "ACE", "AOL", "Apple". Hense, I think in your code , the "output==1' in the function may not be necessary.

However, I don't really know how to edit the code to get what I want to display. Moreover, I ran your code with my tables, there is only 1 company for "A", i.e. "ACE", when there are actually entries containing "ACE", "AOL", "Apple". So need your help again. Thanks a lot. :p
Copy linkTweet thisAlerts:
@DJsACSep 07.2006 — Could you post an example of what the output of the 2/3 pages should be?

I'm trying to guess how you want it, and I don't have a clue as to how your database is set up etc...

I'm understanding the following:

[CODE]Page 1:
-------------------


Visit these companies by clicking the letterlinks below!

A

B

C

D

E

F

G

...

[/CODE]Clicks "A".

[CODE]Page 2:
-------------------


For "A" the companies are: (links)

ACE

AOL

APPLE

ALTAVISTA

ASUS

...

[/CODE]Clicks "APPLE"

[CODE]Page 3:
-------------------


"APPLE" has 4 entries in the database, they are:

APPLE Software

APPLE Hardware and chipsets

MAC (Apple)

The Fruit Company [Apple and Co.]

[/CODE]

Please verifiy this is correct or post your own example...

An example of a company_list and a blog_entry table row would also help ?
Copy linkTweet thisAlerts:
@yunfannyauthorSep 08.2006 — Hi, DJsAC,

Actually I am doing the project on extracting blog entries which containing certain company names by Boolean search.

The company names are stored in table “company_list”, with 2 columes—comp_id, comp_name.

Table “blog_entry” stores the blog entries, with columes of id, title, author, content, url, etc.


Page 1 -- displaying the initial of company names, which are matched in blog entries. For example, “ACE”, “AOL”, “Apple” are found in some blog entries, so “A” will be displayed. While all the company names starting with “T” from company_list are not found in any blog entry, so “T” will not display.

i.e. [U]A[/U] [U]B[/U] [U]C[/U] [U]E[/U] [U]F[/U] [U]G[/U] [U]S[/U] [U]W[/U]


Page 2 – clicking on the letter gives a list of the companies. For example, clicking “A” gives “ACE”, “AOL”, “Apple”. Note that “Accenture” is a company name in the database, but it is not found in any blog entries, so “Accenture” will not be displayed.

i.e, [U]A[/U] [U]B[/U] [U]C[/U] [U]E[/U] [U]F[/U] [U]G[/U] [U]S[/U] [U]W[/U]

[U]ACE[/U]

[U]AOL[/U]

[U]APPLE[/U]

[U]AVA[/U]


Page 3 – clicking the company name gives the list of titles of related blog entries. For example, clicking “Apple” gives the following:

[U]A[/U] [U]B[/U] [U]C[/U] [U]E[/U] [U]F[/U] [U]G[/U] [U]S[/U] [U]W[/U]

[U]ACE[/U]

[U]AOL[/U]

[U]APPLE[/U]

[U]Microsoft Pushes Alternative to JPEG: Windows Media Photo [/U]

[U]Another England Match, Another iPod Launch[/U]

[U]New Holland "Classic" Video iPod Contest[/U]

[U]AVA[/U]


Page 4—a new window appearing to display the blog entry of the blog content when the title is clicked.

So now my problem is still on page 1, to display the letters.

Thank you very much. ?[list]
[/list]
Copy linkTweet thisAlerts:
@yunfannyauthorSep 13.2006 — Hi, DJsAC,

Got any ideas on how to solve my problem ??? I am still struggling... ?

Or anyone else...?

Thanks a lot. ? ?
Copy linkTweet thisAlerts:
@DJsACSep 13.2006 — the line[code=php]$letter_output = mysql_fetch_array($letter_result); [/code] in the code I posted causes the script to only return the first row it finds, If you want to list all the rows in the table that fit the select, you should use a while or for statement so you can loop through all the rows and output them to the webpage.

I can't tell you much more than that, because I still don't understand how the system is supposed to work. Good luck!
×

Success!

Help @yunfanny spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...