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