hyperlink list from database columns
I have a mysql database with my movies
id | movietitle | year | imdbnumber
I want to display my movies as a hyperlink list with a php sql query
like this
<a href="http://www.imdb.com/title/ttIMDBNUMBER/" target="_blank">MOVIETITLE (YEAR)</a><br>
i want them sorted and grouped by letter
Example:
T
Titanic (1997)
THX 1138 (1971)
U
Underworld (2003)
...
how can I do that in php?
php html mysql hyperlink
add a comment |
I have a mysql database with my movies
id | movietitle | year | imdbnumber
I want to display my movies as a hyperlink list with a php sql query
like this
<a href="http://www.imdb.com/title/ttIMDBNUMBER/" target="_blank">MOVIETITLE (YEAR)</a><br>
i want them sorted and grouped by letter
Example:
T
Titanic (1997)
THX 1138 (1971)
U
Underworld (2003)
...
how can I do that in php?
php html mysql hyperlink
1
what have you tried / what's your PHP level (have no clue, beginner, intermediate, etc.)?
– rlatief
Feb 17 '13 at 6:15
php.net/manual/en/book.pdo.php
– Jon
Feb 17 '13 at 6:15
MySQLi might be easier initially. Then PDO.
– Achrome
Feb 17 '13 at 6:19
1
Haha, ok, then: php.net/manual/en/book.mysqli.php
– Jon
Feb 17 '13 at 6:20
add a comment |
I have a mysql database with my movies
id | movietitle | year | imdbnumber
I want to display my movies as a hyperlink list with a php sql query
like this
<a href="http://www.imdb.com/title/ttIMDBNUMBER/" target="_blank">MOVIETITLE (YEAR)</a><br>
i want them sorted and grouped by letter
Example:
T
Titanic (1997)
THX 1138 (1971)
U
Underworld (2003)
...
how can I do that in php?
php html mysql hyperlink
I have a mysql database with my movies
id | movietitle | year | imdbnumber
I want to display my movies as a hyperlink list with a php sql query
like this
<a href="http://www.imdb.com/title/ttIMDBNUMBER/" target="_blank">MOVIETITLE (YEAR)</a><br>
i want them sorted and grouped by letter
Example:
T
Titanic (1997)
THX 1138 (1971)
U
Underworld (2003)
...
how can I do that in php?
php html mysql hyperlink
php html mysql hyperlink
edited Nov 13 '18 at 5:09
Cœur
17.6k9105145
17.6k9105145
asked Feb 17 '13 at 6:12
user2079829user2079829
1
1
1
what have you tried / what's your PHP level (have no clue, beginner, intermediate, etc.)?
– rlatief
Feb 17 '13 at 6:15
php.net/manual/en/book.pdo.php
– Jon
Feb 17 '13 at 6:15
MySQLi might be easier initially. Then PDO.
– Achrome
Feb 17 '13 at 6:19
1
Haha, ok, then: php.net/manual/en/book.mysqli.php
– Jon
Feb 17 '13 at 6:20
add a comment |
1
what have you tried / what's your PHP level (have no clue, beginner, intermediate, etc.)?
– rlatief
Feb 17 '13 at 6:15
php.net/manual/en/book.pdo.php
– Jon
Feb 17 '13 at 6:15
MySQLi might be easier initially. Then PDO.
– Achrome
Feb 17 '13 at 6:19
1
Haha, ok, then: php.net/manual/en/book.mysqli.php
– Jon
Feb 17 '13 at 6:20
1
1
what have you tried / what's your PHP level (have no clue, beginner, intermediate, etc.)?
– rlatief
Feb 17 '13 at 6:15
what have you tried / what's your PHP level (have no clue, beginner, intermediate, etc.)?
– rlatief
Feb 17 '13 at 6:15
php.net/manual/en/book.pdo.php
– Jon
Feb 17 '13 at 6:15
php.net/manual/en/book.pdo.php
– Jon
Feb 17 '13 at 6:15
MySQLi might be easier initially. Then PDO.
– Achrome
Feb 17 '13 at 6:19
MySQLi might be easier initially. Then PDO.
– Achrome
Feb 17 '13 at 6:19
1
1
Haha, ok, then: php.net/manual/en/book.mysqli.php
– Jon
Feb 17 '13 at 6:20
Haha, ok, then: php.net/manual/en/book.mysqli.php
– Jon
Feb 17 '13 at 6:20
add a comment |
2 Answers
2
active
oldest
votes
You can sort the list using the SQL query itself using ORDER BY
.
Once you have the data out of the DB, you can create each of the hyperlinks like this:
<a href="http://www.imdb.com/title/tt<?=$imdbnumber?>/" target="_blank"><?=$movietitle?> <?=$year?></a><br>
add a comment |
If you don't want to use regular expressions, this is one of the solutions.
<?php
$movie[0]='Amber';
$movie[1]='babar';
$movie[2]='Aviator';
$movie[3]='Caty boy';
function startsWith($haystack, $needle)
{
return !strncmp($haystack, $needle, strlen($needle));
}
foreach (range('a', 'z') as $i){
echo '<h1>'.strtoupper($i).'</h1>';
foreach($movie as $key){
if(startsWith(strtolower($key),$i)){
echo '<li>'.$key.'</li>';
}
}
}
?>
Output for above would be :
A
Amber
Aviator
B
Babar
C
Caty boy
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
Populate your $movie array from the database of course with ORDER BY SQL query. Then $movie array can be in this format
Title - (Year)
It won't make a difference what you put after the title - year or any other detail. The condition is that your string should start with the title. SO even if you miss out the format, Ex:
$movie[0]='Titanic';
$movie[1]='Hulk (2009)';
still the function should sort it in the manner you described in your problem statement. Hope it helps.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f14918509%2fhyperlink-list-from-database-columns%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can sort the list using the SQL query itself using ORDER BY
.
Once you have the data out of the DB, you can create each of the hyperlinks like this:
<a href="http://www.imdb.com/title/tt<?=$imdbnumber?>/" target="_blank"><?=$movietitle?> <?=$year?></a><br>
add a comment |
You can sort the list using the SQL query itself using ORDER BY
.
Once you have the data out of the DB, you can create each of the hyperlinks like this:
<a href="http://www.imdb.com/title/tt<?=$imdbnumber?>/" target="_blank"><?=$movietitle?> <?=$year?></a><br>
add a comment |
You can sort the list using the SQL query itself using ORDER BY
.
Once you have the data out of the DB, you can create each of the hyperlinks like this:
<a href="http://www.imdb.com/title/tt<?=$imdbnumber?>/" target="_blank"><?=$movietitle?> <?=$year?></a><br>
You can sort the list using the SQL query itself using ORDER BY
.
Once you have the data out of the DB, you can create each of the hyperlinks like this:
<a href="http://www.imdb.com/title/tt<?=$imdbnumber?>/" target="_blank"><?=$movietitle?> <?=$year?></a><br>
answered Feb 17 '13 at 6:20
ATOzTOAATOzTOA
19.4k166699
19.4k166699
add a comment |
add a comment |
If you don't want to use regular expressions, this is one of the solutions.
<?php
$movie[0]='Amber';
$movie[1]='babar';
$movie[2]='Aviator';
$movie[3]='Caty boy';
function startsWith($haystack, $needle)
{
return !strncmp($haystack, $needle, strlen($needle));
}
foreach (range('a', 'z') as $i){
echo '<h1>'.strtoupper($i).'</h1>';
foreach($movie as $key){
if(startsWith(strtolower($key),$i)){
echo '<li>'.$key.'</li>';
}
}
}
?>
Output for above would be :
A
Amber
Aviator
B
Babar
C
Caty boy
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
Populate your $movie array from the database of course with ORDER BY SQL query. Then $movie array can be in this format
Title - (Year)
It won't make a difference what you put after the title - year or any other detail. The condition is that your string should start with the title. SO even if you miss out the format, Ex:
$movie[0]='Titanic';
$movie[1]='Hulk (2009)';
still the function should sort it in the manner you described in your problem statement. Hope it helps.
add a comment |
If you don't want to use regular expressions, this is one of the solutions.
<?php
$movie[0]='Amber';
$movie[1]='babar';
$movie[2]='Aviator';
$movie[3]='Caty boy';
function startsWith($haystack, $needle)
{
return !strncmp($haystack, $needle, strlen($needle));
}
foreach (range('a', 'z') as $i){
echo '<h1>'.strtoupper($i).'</h1>';
foreach($movie as $key){
if(startsWith(strtolower($key),$i)){
echo '<li>'.$key.'</li>';
}
}
}
?>
Output for above would be :
A
Amber
Aviator
B
Babar
C
Caty boy
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
Populate your $movie array from the database of course with ORDER BY SQL query. Then $movie array can be in this format
Title - (Year)
It won't make a difference what you put after the title - year or any other detail. The condition is that your string should start with the title. SO even if you miss out the format, Ex:
$movie[0]='Titanic';
$movie[1]='Hulk (2009)';
still the function should sort it in the manner you described in your problem statement. Hope it helps.
add a comment |
If you don't want to use regular expressions, this is one of the solutions.
<?php
$movie[0]='Amber';
$movie[1]='babar';
$movie[2]='Aviator';
$movie[3]='Caty boy';
function startsWith($haystack, $needle)
{
return !strncmp($haystack, $needle, strlen($needle));
}
foreach (range('a', 'z') as $i){
echo '<h1>'.strtoupper($i).'</h1>';
foreach($movie as $key){
if(startsWith(strtolower($key),$i)){
echo '<li>'.$key.'</li>';
}
}
}
?>
Output for above would be :
A
Amber
Aviator
B
Babar
C
Caty boy
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
Populate your $movie array from the database of course with ORDER BY SQL query. Then $movie array can be in this format
Title - (Year)
It won't make a difference what you put after the title - year or any other detail. The condition is that your string should start with the title. SO even if you miss out the format, Ex:
$movie[0]='Titanic';
$movie[1]='Hulk (2009)';
still the function should sort it in the manner you described in your problem statement. Hope it helps.
If you don't want to use regular expressions, this is one of the solutions.
<?php
$movie[0]='Amber';
$movie[1]='babar';
$movie[2]='Aviator';
$movie[3]='Caty boy';
function startsWith($haystack, $needle)
{
return !strncmp($haystack, $needle, strlen($needle));
}
foreach (range('a', 'z') as $i){
echo '<h1>'.strtoupper($i).'</h1>';
foreach($movie as $key){
if(startsWith(strtolower($key),$i)){
echo '<li>'.$key.'</li>';
}
}
}
?>
Output for above would be :
A
Amber
Aviator
B
Babar
C
Caty boy
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
Populate your $movie array from the database of course with ORDER BY SQL query. Then $movie array can be in this format
Title - (Year)
It won't make a difference what you put after the title - year or any other detail. The condition is that your string should start with the title. SO even if you miss out the format, Ex:
$movie[0]='Titanic';
$movie[1]='Hulk (2009)';
still the function should sort it in the manner you described in your problem statement. Hope it helps.
edited Feb 17 '13 at 8:48
answered Feb 17 '13 at 6:36
harsh8888harsh8888
422821
422821
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f14918509%2fhyperlink-list-from-database-columns%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
what have you tried / what's your PHP level (have no clue, beginner, intermediate, etc.)?
– rlatief
Feb 17 '13 at 6:15
php.net/manual/en/book.pdo.php
– Jon
Feb 17 '13 at 6:15
MySQLi might be easier initially. Then PDO.
– Achrome
Feb 17 '13 at 6:19
1
Haha, ok, then: php.net/manual/en/book.mysqli.php
– Jon
Feb 17 '13 at 6:20