Solar Posted April 12, 2010 Share Posted April 12, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/198302-couple-questions/ Share on other sites More sharing options...
Ken2k7 Posted April 12, 2010 Share Posted April 12, 2010 1. Yes, it's possible. Do you want the SQL for that or are you just asking if it's possible? 2. Bad practice. I would just store logo.png and not the full URL. But again, yes, you can trim it. Quote Link to comment https://forums.phpfreaks.com/topic/198302-couple-questions/#findComment-1040461 Share on other sites More sharing options...
the182guy Posted April 12, 2010 Share Posted April 12, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/198302-couple-questions/#findComment-1040465 Share on other sites More sharing options...
ignace Posted April 12, 2010 Share Posted April 12, 2010 For question #2 I would store it like: id, host, path 1, www.phpfreaks.com, logo.png Quote Link to comment https://forums.phpfreaks.com/topic/198302-couple-questions/#findComment-1040467 Share on other sites More sharing options...
Solar Posted April 12, 2010 Author Share Posted April 12, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/198302-couple-questions/#findComment-1040479 Share on other sites More sharing options...
Ken2k7 Posted April 12, 2010 Share Posted April 12, 2010 If $list['url'] contains the full URL, why not remove the "http://phpfreaks.com/" part from the echo? Quote Link to comment https://forums.phpfreaks.com/topic/198302-couple-questions/#findComment-1040481 Share on other sites More sharing options...
Solar Posted April 12, 2010 Author Share Posted April 12, 2010 Sorry I modified my post to make more sense out of it. Quote Link to comment https://forums.phpfreaks.com/topic/198302-couple-questions/#findComment-1040483 Share on other sites More sharing options...
the182guy Posted April 12, 2010 Share Posted April 12, 2010 <?php echo "<img border=\"0\" src=\"http://google.com/" . basename($list['url']) . "\">"; ?> Is that what you're looking for? Quote Link to comment https://forums.phpfreaks.com/topic/198302-couple-questions/#findComment-1040485 Share on other sites More sharing options...
Solar Posted April 12, 2010 Author Share Posted April 12, 2010 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= ? Quote Link to comment https://forums.phpfreaks.com/topic/198302-couple-questions/#findComment-1040505 Share on other sites More sharing options...
the182guy Posted April 12, 2010 Share Posted April 12, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/198302-couple-questions/#findComment-1040509 Share on other sites More sharing options...
Ken2k7 Posted April 12, 2010 Share Posted April 12, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/198302-couple-questions/#findComment-1040510 Share on other sites More sharing options...
the182guy Posted April 12, 2010 Share Posted April 12, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/198302-couple-questions/#findComment-1040511 Share on other sites More sharing options...
Ken2k7 Posted April 12, 2010 Share Posted April 12, 2010 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= ? Quote Link to comment https://forums.phpfreaks.com/topic/198302-couple-questions/#findComment-1040513 Share on other sites More sharing options...
the182guy Posted April 12, 2010 Share Posted April 12, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/198302-couple-questions/#findComment-1040517 Share on other sites More sharing options...
Solar Posted April 12, 2010 Author Share Posted April 12, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/198302-couple-questions/#findComment-1040523 Share on other sites More sharing options...
Ken2k7 Posted April 12, 2010 Share Posted April 12, 2010 Okay, you're asking the wrong questions here. I think you're just storing bad data in your database. Quote Link to comment https://forums.phpfreaks.com/topic/198302-couple-questions/#findComment-1040525 Share on other sites More sharing options...
Solar Posted April 12, 2010 Author Share Posted April 12, 2010 Okay, you're asking the wrong questions here. I think you're just storing bad data in your database. I will make a new topic with a different way then. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/198302-couple-questions/#findComment-1040532 Share on other sites More sharing options...
the182guy Posted April 12, 2010 Share Posted April 12, 2010 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' ?> Quote Link to comment https://forums.phpfreaks.com/topic/198302-couple-questions/#findComment-1040577 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.