PDA

View Full Version : How Do I list from SQL?



sartajc
10-17-2006, 09:27 PM
I am making a PHP thing and have the following if statements:

My SQL i have is this:


CREATE TABLE `jos_mochat` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(100) NOT NULL default '',
`url` text,
`height` varchar(50) NOT NULL default '',
`width` varchar(50) NOT NULL default '',
`auto` int(11) default '0',
`frame` int(11) default '0',
`wlink` int(11) default '0',
`showname` int(11) default '0',
`scroll` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
and i have this code in the PHP

if ((@$_GET['wrap'] == "") && (@$_GET['page'] == "")) {

When this IF statement is excecuted, i need the page to retrieve 'name' from the MySQL and list them. But I also need it to retrieve the 'url' and make the page a link like this :
<a href="chat.muslimonline.org/schat/'url'>'name'</a>

I tried echo statements but messed it up. So can anyone help me?

thetestingsite
10-18-2006, 01:06 AM
Now if the variable "page"
$_GET['page'] is the column "name" that you refer to, then use/modify the following....


<?php
require('config.php'); //assuming you have all the "important" variables in this file

$conn = @mysql_connect($server,$username,$password);
mysql_select_db($db);

if ((@$_GET['wrap'] == "") && (@$_GET['page'] == "")) {
/*no page or info to send to the remainder of the program, echo error message, or whatnot*/
}

else {
$theLink = @mysql_query("SELECT * FROM `$table` WHERE `name`='$_GET[page]'); //request the data from the SQL

$qry = @mysql_fetch_array($theLink);

echo '<a href="http://chat.muslimonline.org/schat/' . $qry[url] . '>' . $qry[name] . '</a>'; //echo the link

}
?>

sartajc
10-18-2006, 05:14 PM
Now if the variable "page" is the column "name" that you refer to, then use/modify the following....


<?php
require('config.php'); //assuming you have all the "important" variables in this file

$conn = @mysql_connect($server,$username,$password);
mysql_select_db($db);

if ((@$_GET['wrap'] == "") && (@$_GET['page'] == "")) {
/*no page or info to send to the remainder of the program, echo error message, or whatnot*/
}

else {
$theLink = @mysql_query("SELECT * FROM `$table` WHERE `name`='$_GET[page]'); //request the data from the SQL

$qry = @mysql_fetch_array($theLink);

echo '<a href="http://chat.muslimonline.org/schat/' . $qry[url] . '>' . $qry[name] . '</a>'; //echo the link

?>


will that bring up all the links?

thetestingsite
10-18-2006, 05:43 PM
The above code will print out only the link that the variable "name" finds in the SQL, if you wanted to show all of the links simply edit this part:



else {
$theLink = @mysql_query("SELECT * FROM `$table` WHERE `name`='$_GET[page]'); //request the data from the SQL

$qry = @mysql_fetch_array($theLink);

echo '<a href="http://chat.muslimonline.org/schat/' . $qry[url] . '>' . $qry[name] . '</a>'; //echo the link

}

to this:


else {
$theLink = @mysql_query("SELECT * FROM `$table` WHERE `name`='$_GET[page]'); //request the data from the SQL

while($qry = @mysql_fetch_array($theLink)) { //go through each record of the sql

echo '<a href="http://chat.muslimonline.org/schat/' . $qry[url] . '>' . $qry[name] . '</a> <BR>'; //echo the info from the record that the while statement is on

} //end while
} //end else


and that should take care of it.

sartajc
10-19-2006, 05:46 PM
eh....the link turned orange cause of the http:// which means its a side note and not the php. how do you make the link work with php?

thetestingsite
10-19-2006, 06:24 PM
Sorry about that, I forgot to do a few minor things, I typed it in a hurry without testing or proofreading and I ended up missing a few quotation marks in the process. The better coding is as follows (this is the one that has been proofread and it should work fine with some minor altering.



<?php
require('config.php'); //assuming you have all the "important" variables in this file

$conn = @mysql_connect($server,$username,$password);
mysql_select_db($db);

if ((@$_GET['wrap'] == "") && (@$_GET['page'] == "")) {
/*no page or info to send to the remainder of the program, echo error message, or whatnot*/
}

else {
$theLink = @mysql_query("SELECT * FROM `$table` WHERE `name`='$_GET[page]'"); //request the data from the SQL

$qry = @mysql_fetch_array($theLink);

echo '<a href="http://chat.muslimonline.org/schat/' . $qry[url] . '">' . $qry[name] . '</a>'; //echo the link

}
?>


or this:



<?php
require('config.php'); //assuming you have all the "important" variables in this file

$conn = @mysql_connect($server,$username,$password);
mysql_select_db($db);

if ((@$_GET['wrap'] == "") && (@$_GET['page'] == "")) {
/*no page or info to send to the remainder of the program, echo error message, or whatnot*/
}

else {
$theLink = @mysql_query("SELECT * FROM `$table` WHERE `name`='$_GET[page]'"); //request the data from the SQL

$qry = @mysql_fetch_array($theLink);

echo '<a href="http://chat.muslimonline.org/schat/' . $qry[url] . '">' . $qry[name] . '</a>'; //echo the link

?>

sartajc
10-21-2006, 07:52 PM
Its not working. If you want to see the PHP component in action, check out http://famousmuslims.muslimonline.org/index.php?option=com_mochat

As you can see, it is just bringing up the error message as defined in my config file, but what i wanted was all the chatrooms to come up when it is on the index page of this component, which explains why i used the
if ((@$_GET['wrap'] == "") && (@$_GET['page'] == "")) { I wanted the list to come up if nothing else comes up, and right now it isnt. here is the code so far:


<?php
//* @version 1.0
// Copyright (C) 2006 Muslim Online (Sartaj Chowdhury)
// Based on Mambo Wrapper Component by Jason Murphy (http://www.jasonmurphy.net)
// All rights reserved.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// The "GNU General Public License" (GPL) is available at
// http://www.gnu.org/copyleft/gpl.html.
//
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

include ("administrator/components/com_mochat/mochat_config.php");
$my_id = $my->id;
if ((@$_GET['wrap'] == "") && (@$_GET['page'] == "")) {

$conn = @mysql_connect($server,$username,$password);
mysql_select_db($db);

$theLink = @mysql_query("SELECT * FROM `$table` WHERE `name`='$_GET[page]'"); //request the data from the SQL

$qry = @mysql_fetch_array($theLink);

echo '<a href="http://chat.muslimonline.org/schat/' . $qry[url] . '">' . $qry[name] . '</a>'; //echo the link



}
if (@$_GET['wrap'] == "") {
$wrap = "";
}
$wrap = str_replace(array( "JOIN", "' ", "WHERE" ),array( "", "", ""), $wrap);
if ($allowpage == "1" && @$_GET['page'] != "") {
$finalurl = $_GET['page'];
$wauto = "$resizepage";
} else {
$page = "";
$database->setQuery( "select url, width, height, scroll, auto, frame, wlink, showname from #__mochat where name='$wrap'" );
$results = $database->loadObjectList();
foreach ($results as $result){
$wurl = $result->url;
$wwidth = $result->width;
$wheight = $result->height;
$wscroll = $result->scroll;
$wauto = $result->auto;
$wframe = $result->frame;
$wlink = $result->wlink;
$wshowname = $result->showname;
}
if (!isset($wurl)){
$wurl = "";
}
$finalurl = "$wurl";
}
if ($my_id == "" && $regonly == "1") {
die($notauthtext);
} else {

if ($finalurl == "") {
echo $werror;
} else {
if (!(eregi("http://chat.muslimonline.org/schat/", $finalurl) || (eregi("https://",$finalurl)))) {
$finalurl = "http://chat.muslimonline.org/schat/".$finalurl;
}
if ($wauto == "1"){
//resize script
$xload = "iFrameHeight()";
?>

<script language="JavaScript">
function iFrameHeight() {
var h = 0;
if(document.getElementById && !(document.all))
{
h = document.getElementById('blockrandom').contentDocument.height;
document.getElementById('blockrandom').style.height = h + 60 + 'px';
}
else if(document.all)
{
h = document.frames('blockrandom').document.body.scrollHeight;
document.all.blockrandom.style.height = h + 20 + 'px';
}
}
</script>
<?php
} else {
$xload = "";
}
if (!isset($wheight)) {$wheight = $dheight; }
if (!isset($wwidth)) {$wwidth = $dwidth; }
if (!isset($wscroll)) {$wscroll = $dscroll; }
if (!isset($wframe)) {$wframe = $dframe; }
if (!isset($wshowname)) {$wshowname = $dshowname; }
if (!isset($wlink)) {$wlink = $dwlink; }
?>
<script language="JavaScript">
function blockError(){return true;}
window.onerror = blockError;
</script>
<IFRAME id="blockrandom"
onLoad="<?php echo $xload; ?>;"
SRC="<?php echo $finalurl; ?>"
width="<?php echo $wwidth; ?>" height="<?php echo $wheight; ?>" align=top scrolling=<?php echo $wscroll; ?> frameborder=<?php echo $wframe; ?>>Sorry, your Browser does NOT SUPPORT IFRAME.<br>You may want to Upgrade your Browser.<br><a href="<?php echo $finalurl; ?>" target="_blank">Click Here to see the page that was supposed to load.</a><?php echo $finalurl; ?></IFRAME>&nbsp<a href="http://chat.muslimonline.org">&nbsp;</a>
<div align="center">
<?php
if ($wshowname == "1") {
echo $finalurl;
}
?>
<br>
<?php
if ($wlink == "1") {
?>
<a href="<?php echo $finalurl; ?>" target="_blank">[<?php echo $newwindowtext; ?>]</a></div>
<?php
}
}
}
?>


btw, everything in the component is working except this index page, for example see a chatroom in action: http://famousmuslims.muslimonline.org/index.php?option=com_mochat&wrap=test

thetestingsite
10-21-2006, 08:26 PM
I see, so if the user has not selected anything, by default show all of the chats. For this change the following:


$theLink = @mysql_query("SELECT * FROM `$table` WHERE `name`='$_GET[page]'");

to this:


$theLink = @mysql_query("SELECT * FROM `$table`");

sartajc
10-26-2006, 02:42 AM
I see, so if the user has not selected anything, by default show all of the chats. For this change the following:


$theLink = @mysql_query("SELECT * FROM `$table` WHERE `name`='$_GET[page]'");

to this:


$theLink = @mysql_query("SELECT * FROM `$table`");

hmm...didn't work.

Here is an interesting thing I found. In the admin version of this chat where you can change the settings, i found this code:

admin.mochat.php

/**
* List the records
* @param string The current GET/POST option
*/
function showContacts( $option ) {
global $database, $mainframe;




$database->setQuery( "SELECT * FROM #__mochat"
);
$rows = $database->loadObjectList();

HTML_contact::showcontacts( $rows, $option );
}

And then in admin.mochat.html.php i found this code related to the list:


// ensure this file is being included by a parent file
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

class HTML_contact {

function showContacts( &$rows, $option ) {
?>
<form action="index2.php" method="post" name="adminForm">
<table cellpadding="4" cellspacing="0" border="0" width="100%">
<tr>
<td width="100%" class="sectionname">Wrap Manager</td>


</tr>
</table>
<table cellpadding="4" cellspacing="0" border="0" width="100%" class="adminlist">
<tr>
<th width="20" nowrap="nowrap">#</th>
<th width="20" class="title" nowrap="nowrap"><input type="checkbox" name="toggle" value="" onclick="checkAll(<?php echo count($rows); ?>);" /></th>
<th width="20%" class="title" nowrap="nowrap">NAME</th>
<th width="20%" class="title" nowrap="nowrap">CHATROOM</th>
<th width="10%" class="title" nowrap="nowrap">HEIGHT</th>
<th width="10%" class="title" nowrap="nowrap">WIDTH</th>
<th width="50" class="title" nowrap="nowrap">AUTOSIZE</th>
<th width="50" class="title" nowrap="nowrap">FRAME</th>
<th width="10%" class="title" nowrap="nowrap">SHOW LINK</th>
<th width="10%" class="title" nowrap="nowrap">SHOW NAME</th>
<th width="50" class="title" nowrap="nowrap">SCROLL</th>

</tr>
<?php
$k = 0;
for ($i=0, $n=count($rows); $i < $n; $i++) {
$row = $rows[$i];
$img = $row->showname ? 'tick.png' : 'publish_x.png';
$task = $row->showname ? 'unpublish' : 'publish';
$pageNav = 0;
?>
<tr class="<?php echo "row$k"; ?>">
<td width="20"><?php echo $i+1+$pageNav->limitstart; ?></td>
<td width="20"><input type="checkbox" id="cb<?php echo $i;?>" name="cid[]" value="<?php echo $row->id; ?>" onclick="isChecked(this.checked);" /></td>
<td width="20%"><a href="#edit" onclick="return listItemTask('cb<?php echo $i;?>','edit')"><?php echo $row->name; ?></a></td>
<td width="20%"><?php echo $row->url; ?></td>
<td width="10%"><?php echo $row->height; ?></td>
<td width="10%"><?php echo $row->width; ?></td>
<?php if ($row->auto) { ?>
<td width="50" align="center"><img src="images/tick.png"></td>
<?php } else { ?>
<td width="50" align="center">&nbsp;</td>
<?php } ?>
<?php if ($row->frame) { ?>
<td width="50" align="center"><img src="images/tick.png"></td>
<?php } else { ?>
<td width="50" align="center">&nbsp;</td>
<?php } ?>
<?php if ($row->wlink) { ?>
<td width="10%" align="center"><img src="images/tick.png"></td>
<?php } else { ?>
<td width="10%" align="center">&nbsp;</td>
<?php } ?>
<?php if ($row->showname) { ?>
<td width="10%" align="center"><img src="images/tick.png"></td>
<?php } else { ?>
<td width="10%" align="center">&nbsp;</td>
<?php } ?>
<td width="20%"><?php echo $row->scroll; ?></td>


</tr>
<?php $k = 1 - $k;
} ?>

</table>
Problem is that mochat.php doesnt have a mochat.html.php . hmmm...Does this help at all?

thetestingsite
10-27-2006, 12:39 AM
is there any way that you could send me the entire program and I could take a look at it to see if I can figure it out. The two pieces of code above don't help out that well...sorry

sartajc
10-27-2006, 04:59 AM
http://www.rogepost.com/dn/wige . theres all the files. remember it is a Joomla Component, which explains alot of the weird non PHP code and the xml file.

sartajc
11-02-2006, 09:24 PM
anybody there to help?

sartajc
11-05-2006, 10:38 PM
Hello...can anyone help?