hald Posted May 13, 2008 Share Posted May 13, 2008 Hello everyone, I am currently administering a business directory that I'm having a small issue with. We have businesses and nonprofit orgs in the area separated by a 1(for business) or 2(for nonprofit) in a column labeled `type` in our database. I am trying to fix an issue the previous webmaster left. The nonprofits are linked under a separate URL(\nonprofits.php?profile=$theID) from the businesses(\directory.php?profile=$theID), but in the config.php file, only the business URL is listed, so it is used for nonprofits as well, which links to a blank page. I was trying to create an if-then statement where it would read something like: if(type==1){ $output .= "THE BUSINESS URL";} else{ $output .= "THE NONPROFIT URL"; } However, I cannot come up with an SQL statement that will return the value in the column `type`. If anyone thinks they can help, I can explain any questions you have. Please, let me know! I appreciate any help very, very much! Quote Link to comment Share on other sites More sharing options...
soycharliente Posted May 13, 2008 Share Posted May 13, 2008 SELECT * FROM table WHERE type='1' gives you all the business URLs change to 2 to get the non-profit. Is that what you were looking for? Or how to get it for each specific entry? Quote Link to comment Share on other sites More sharing options...
hald Posted May 13, 2008 Author Share Posted May 13, 2008 That syntax is familiar to me. I was trying something like that. IS there a way I can integrate that into a for statement...something like: $type1 = "SELECT * FROM table WHERE type='1'"; $type2 = "SELECT * FROM table WHERE type='2''"; for $type1 ($output = x); for $type2 ($output =y); Quote Link to comment Share on other sites More sharing options...
soycharliente Posted May 13, 2008 Share Posted May 13, 2008 What data do you want to change and how do you want to output it? Transform the number into something? Or also grab the URL at the same time and show it? Quote Link to comment Share on other sites More sharing options...
hald Posted May 13, 2008 Author Share Posted May 13, 2008 What we have is a (clike here to read more) link. I don't want to change any data, but the links for businesses are different than the ones for nonprofits. Currently, we have a statement that covers both, but because the nonprofits aren't linked to in the same way as businesses, it doesn't work for them. What I want to do is: for all profiles that are type 1, use one type of hyperlink, and for all type 2 profiles, use another. Quote Link to comment Share on other sites More sharing options...
soycharliente Posted May 13, 2008 Share Posted May 13, 2008 <?php $sql = "SELECT * FROM table"; $result = mysql_query($sql) OR DIE (mysql_error()); if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_array($result)) { // add more variable here to get other column fields $type = $row['type']; if ($type == 1) { echo "THE LINK"; } else if ($type == 2) { echo "THE OTHER LINK"; } } } else { echo "No rows returned."; } ?> Does that help? Quote Link to comment Share on other sites More sharing options...
hald Posted May 13, 2008 Author Share Posted May 13, 2008 $conn = mysql_connect('database','usrname','passwrd'); mysql_select_db('knoxgreen', $conn); $sql = "SELECT * FROM directory"; $result = mysql_query($sql) OR DIE (mysql_error()); $length = 50; $paragraph = explode(" ", $string); if(mysql_num_rows($result) > 0) { if($length < count($paragraph)){ for($i = 0; $i < $length; $i++){ if($i < $length - 1){ $output .= $paragraph[$i] . " "; }else{ while($row = mysql_fetch_array($result)) { $type = $row['type']; if($type == '1'){ if($searched==''){ $output .= $paragraph[$i] . " ... <b><a class=\"noline\" href=\"directory.php?profile=$theID\">( click here to read more )</a></b>"; break; }else{ $output .= $paragraph[$i] . " ... <b><a class=\"noline\" href=\"directory.php?profile=$theID&searched=$searched\">( click here to read more )</a></b>"; break; }} elseif ($type == '2'){$output .= $paragraph[$i] . " ... <b><a class=\"noline\" href=\"nonprofits.php?profile=$theID\">( click here to read more )</a></b>"; break; } }}}} return $output; } return $string; } I think it's getting close, but it still isn't separating the two types. Quote Link to comment Share on other sites More sharing options...
soycharliente Posted May 13, 2008 Share Posted May 13, 2008 it's really hard to pick through your nested if and else statements because it's not formatted in a readable way. Quote Link to comment Share on other sites More sharing options...
hald Posted May 13, 2008 Author Share Posted May 13, 2008 function wordlimit($string, $theID, $searched){ $conn = mysql_connect('********','*******','*********'); mysql_select_db('knoxgreen', $conn); $sql = "SELECT * FROM directory"; $result = mysql_query($sql) OR DIE (mysql_error()); $length = 50; $paragraph = explode(" ", $string); if(mysql_num_rows($result) > 0){ if($length < count($paragraph)){ for($i = 0; $i < $length; $i++){ if($i < $length - 1){ $output .= $paragraph[$i] . " "; }else{ while($row = mysql_fetch_array($result)) { $type = $row['type']; if($type == '1'){ if($searched==''){ $output .= $paragraph[$i] . " ... <b><a class=\"noline\" href=\"directory.php?profile=$theID\">( click here to read more )</a></b>"; break; }else{ $output .= $paragraph[$i] . " ... <b><a class=\"noline\" href=\"directory.php?profile=$theID&searched=$searched\">( click here to read more )</a></b>"; break; } } elseif ($type == '2') {$output .= $paragraph[$i] . " ... <b><a class=\"noline\" href=\"nonprofits.php?profile=$theID\">( click here to read more )</a></b>"; break; } } } } } return $output; } return $string; } I hope that's better . I've never really had to care about format (although that's probably why my code's always unorganized), so I hope this is a bit more readable. Quote Link to comment Share on other sites More sharing options...
soycharliente Posted May 13, 2008 Share Posted May 13, 2008 Everything looks fine to me. Try echoing out "type" to see exactly what you're getting and checking against from the db. Quote Link to comment Share on other sites More sharing options...
hald Posted May 13, 2008 Author Share Posted May 13, 2008 I echoed out $type and it shows a 1 under each, including nonprofits. I went into phpmyadmin just to make sure, and they are still set to 2. Quote Link to comment Share on other sites More sharing options...
soycharliente Posted May 13, 2008 Share Posted May 13, 2008 change elseif to else if Quote Link to comment Share on other sites More sharing options...
soycharliente Posted May 13, 2008 Share Posted May 13, 2008 You can use else, but I said else if in case the db accidentally got a different number besides 1 or 2. Also, if you added another type, it would be much easier to update. Quote Link to comment Share on other sites More sharing options...
hald Posted May 13, 2008 Author Share Posted May 13, 2008 Ah, thanks...typo. Fixed it, but still didn't help >.< Add another type? Quote Link to comment Share on other sites More sharing options...
soycharliente Posted May 13, 2008 Share Posted May 13, 2008 Yeah. You have types 1 and 2. IF you added a 3, already having an else if would make it easy to add that in. Quote Link to comment Share on other sites More sharing options...
hald Posted May 13, 2008 Author Share Posted May 13, 2008 Oh I see what you're saying. Yeah, I can definitely do that if I need to. I really want to figure out why it's globalizing type 1 though. Quote Link to comment Share on other sites More sharing options...
soycharliente Posted May 13, 2008 Share Posted May 13, 2008 Is it only outputting the nonprofit ones? Or is it outputting all types but with the wrong link format? Quote Link to comment Share on other sites More sharing options...
soycharliente Posted May 13, 2008 Share Posted May 13, 2008 nevermind. deleted what I said. Quote Link to comment Share on other sites More sharing options...
hald Posted May 13, 2008 Author Share Posted May 13, 2008 It is outputting the directory.php hyperlink to all of them, and when I echo out $type, even the nonprofits register as a 1. Quote Link to comment Share on other sites More sharing options...
soycharliente Posted May 13, 2008 Share Posted May 13, 2008 So you are testing locally or remotely? did you upload the correct file? I honestly don't see anything in your code that would make $type get set to 1 for every instance. You said that you looked at the data in phpMyAdmin and you can plainly see 2 as a value. Maybe someone else will read this thread and a new pair of eyes will find something I can't. Quote Link to comment Share on other sites More sharing options...
hald Posted May 13, 2008 Author Share Posted May 13, 2008 I uploaded the correct file and I am testing on the website while it's online. 1 was previously set as the default value, so I changed that and it still does the same thing. Quote Link to comment Share on other sites More sharing options...
hald Posted May 13, 2008 Author Share Posted May 13, 2008 here's something interesting though...i changed the first if ($type == 1) to if ($type == 2), and they all displayed 2. Quote Link to comment 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.