Jump to content

Couple Questions


Solar

Recommended Posts

Question #1:

Lets say if I have a database that looks like this;

id

username

gender

1

Steven

Male

2

Emily

Female

3

Jason

Male

 

Is it possible to SELECT gender and just list the column that has male? How would I go by doing so?

 

 

Question #2

Lets say I have a database like this;

idnameurl

1phpfreakshttp://phpfreaks.com/logo.png

 

Is it possible; After Selecting the database, To when you list the row "URL" to trim http://phpfreaks.com/ to just show logo.png? How would I go by doing so?

 

Thanks a lot for your time and thankyou for replying below in this topic.

Link to comment
https://forums.phpfreaks.com/topic/198302-couple-questions/
Share on other sites

Question #1:

Lets say if I have a database that looks like this;

id

username

gender

1

Steven

Male

2

Emily

Female

3

Jason

Male

 

Is it possible to SELECT gender and just list the column that has male? How would I go by doing so?

 

Yes of course that it is possible. Example query

 

SELECT username FROM table WHERE gender = 'Male'

 

Tutorial: http://www.tizag.com/mysqlTutorial/mysqlwhere.php

 

Question #2

Lets say I have a database like this;

idnameurl

1phpfreakshttp://phpfreaks.com/logo.png

 

Is it possible; After Selecting the database, To when you list the row "URL" to trim http://phpfreaks.com/ to just show logo.png? How would I go by doing so?

 

Thanks a lot for your time and thankyou for replying below in this topic.

 

You can use the basename() function to do that. Example:

 


$url = 'http://phpfreaks.com/logo.png';

$filename = basename($url);

// $filename is logo.png

Link to comment
https://forums.phpfreaks.com/topic/198302-couple-questions/#findComment-1040465
Share on other sites

Thanks for the fast replies! I really appreciate it. Answer to Question 1 works like a charm, I even used WHERE gender = '" . htmlspecialchars($_GET["name"]) . "' and works like a charm.

 

I want to go more in depth with Question # 2.

 

Say I have a script like this;

 

<?php echo $list[''] . "<img border=\"0\" src=\"http://google.com/" . $list['url'] . "\">"; ?>

 

And what comes out is;

http://google.com/http://phpfreaks.com/logo.png

 

Using;

$url = 'http://phpfreaks.com/logo.png';
$filename = basename($url);// 
$filename is logo.png

 

How would I import that into my code?

 

Better explination;

So when a user submits a form

It asks for URL

Instead of trimming it to logo.png on the spot, I would like to trim the URL when the list shows.

 

So the end results turn into;

http://google.com/logo.png

Link to comment
https://forums.phpfreaks.com/topic/198302-couple-questions/#findComment-1040479
Share on other sites

Exactly what I am looking for the182guy! Thanks a lot for all of your help everyone who has posted!

 

One very last Question for future refrence.

 

id

name

url

1

phpfreaks

http://phpfreaks.com/profile?photo=php

 

Using;

$url = "http://phpfreaks.com/profile?photo=";
$filename = basename($url);

 

How would I cut out /profile?photo= ?

Link to comment
https://forums.phpfreaks.com/topic/198302-couple-questions/#findComment-1040505
Share on other sites

That can be done like this

 

<?php
$url = 'http://phpfreaks.com/profile?photo=php';

$index = strpos($url, '?'); // find the location of ? (if any)

if($index !== false) // does the URL have a question mark in it?
{
$url = substr($url, 0, $index);
}

echo $url; // $url is http://phpfreaks.com/profile

?>

 

Or another way...

 

$url = 'http://phpfreaks.com/profile?photo=php';

$index = strpos($url, '?'); // find the location of ? (if any)

if($index !== false) // does the URL have a question mark in it?
{
$temp = expode('?', $url);
$url = $temp[0];
}

echo $url; // $url is http://phpfreaks.com/profile

Link to comment
https://forums.phpfreaks.com/topic/198302-couple-questions/#findComment-1040509
Share on other sites

Since we are tossing up ideas...

 

<?php
$url = 'http://phpfreaks.com/profile?photo=php';

$parts = parse_url($url);

echo sprintf("%s://%s", $parts['scheme'], $parts['host']);

 

You can obviously find out anything you want in a url with parse_url. ;)

 

and $parts['path'] otherwise it would just echo http://phpfreaks.com :)

Link to comment
https://forums.phpfreaks.com/topic/198302-couple-questions/#findComment-1040511
Share on other sites

Since we are tossing up ideas...

 

<?php
$url = 'http://phpfreaks.com/profile?photo=php';

$parts = parse_url($url);

echo sprintf("%s://%s", $parts['scheme'], $parts['host']);

 

You can obviously find out anything you want in a url with parse_url. ;)

 

and $parts['path'] otherwise it would just echo http://phpfreaks.com :)

Yes. That's what he asked for.

 

How would I cut out /profile?photo= ?
Link to comment
https://forums.phpfreaks.com/topic/198302-couple-questions/#findComment-1040513
Share on other sites

Oops, thought he was wanting to drop the querystring only, after re-reading I can see you're right.

 

In that case parse_url would be simpler to use because strpos/substr would get slightly more complicated because of havng to check for both the forward slash and the question mark.

Link to comment
https://forums.phpfreaks.com/topic/198302-couple-questions/#findComment-1040517
Share on other sites

Thanks for the replies and I was working on it and it is what I am looking for, somewhat.

 

The database URL would be;

'http://phpfreaks.com/profile?photo=php'

 

 

When it echo's I would like it to transform to;

'php'

 

So pretty much take out (Is what I meant);

'http://phpfreaks.com/profile?photo='

 

Thanks for the replies so far, its just great to learn!!, and the codes you've been posting I have an idea for those for my site as well!

Link to comment
https://forums.phpfreaks.com/topic/198302-couple-questions/#findComment-1040523
Share on other sites

Thanks for the replies and I was working on it and it is what I am looking for, somewhat.

 

The database URL would be;

'http://phpfreaks.com/profile?photo=php'

 

 

When it echo's I would like it to transform to;

'php'

 

So pretty much take out (Is what I meant);

'http://phpfreaks.com/profile?photo='

 

Thanks for the replies so far, its just great to learn!!, and the codes you've been posting I have an idea for those for my site as well!

 

In that case you could use parse_url to extract the querystring, then use parse_str to further extract the photo variabe like

 

<?php

$url = 'http://www.phpfreaks.com/profile?photo=php&test=test1';

$urlInfo = parse_url($url);

$output = array();

parse_str($urlInfo['query'], $output);

echo $output['photo']; // shows 'php'

?>

Link to comment
https://forums.phpfreaks.com/topic/198302-couple-questions/#findComment-1040577
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.