News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

whats wrong with this code?

Started by kL, October 28, 2006, 01:20:35 PM

Previous topic - Next topic

kL

index.php...
<?php require("/home/ktalk/public_html/forums/SSI.php"); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>.::KanYeTalk::.</title>
<?php 

$dbu
='***'//database username
$dbp='***'//database password
$dbn='***'//database name
mysql_connect(localhost,$dbu,$dbp);
@
mysql_select_db($dbn) or die( "Unable to select database");?>


<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#CCCCCC">

<div align="center">

<?php $act=$_REQUEST['act']; if ($act=='home') { echo 'Welcome to KanYeTalk.com'; }
if (
$act=='news') { if ($context['user']['is_admin']) {echo '<a href="?act=news&do=add">Add News</a>';}}


  
 if (
$act == '' ) {$act='home';}
  
$act.='.php';
  include(
$act); ?>

</div>
<?php mysql_close(); ?>


home.php...


<?php
$query
="SELECT COUNT(*) FROM news";
mysql_query($query);
$result=mysql_query($query);
$num=mysql_num_rows($result);
$a=1;
$i=$num;
while (
$i 0) {

$newsid=mysql_result($result,$i,"newsid");
$newstitle=mysql_result($result,$i,"newstitle");
$newscontent=mysql_result($result,$i,"newscontent");
$newsauthor=mysql_result($result,$i,"newsauthor");
$newsmonth=mysql_result($result,$i,"newsmonth");
$newsday=mysql_result($result,$i,"newsday");
$newsyear=mysql_result($result,$i,"newsyear");
$newscontentprvw=mysql_result($result,$i,"newscontentprvw");
$newsshowarticle=mysql_result($result,$i,"newsshowarticle");
$newscontentprvw.='...';
if ( 
$newshowarticle==) {

echo 
'<tr>
          <td valign="top" class="mytext2"><strong>'
$newstitle'</strong><span class="mytext"><br />
            '
$newscontentprvw'</span><br />
            <img src="images/bul.gif" width="4" height="6" /> <a href="?act=news&id='
$newsid'">Read More</a></td>
        </tr>'
;


$a++;

if ($a == ) {$i 0; } 

$i $i 1;
}}


$rp =  ssi_recentPosts(3, array('8.0'), 'array');
foreach (
$rp as $post)
{

echo 
'<tr>       
  <td valign="top" class="mytext2"><strong>'
$post['subject'], '</strong><span class="mytext"><br />
            this will load 135 characters of the post from the database and show them here blah blah yes it will etc but i havent done that yet </span><br />
            <img src="images/bul.gif" width="4" height="6" /> <a href="'
$post['href'], '">Read More</a></td>
           </tr>'
;
}




?>


i removed a lot of html but you get the idea. what happens when i try this isthe page takes a LONG time to load, sometimes it doesnt even load, especially the home.php part, (the index loads ok, but still a bit slow) yet all my other pages work fine, including my smf forum. :'( you can see it at : www.kanyetalk.com/st/index.php?act=home

kL

ok when the home.php finally loads this is the text i get:

Warning: mysql_result() [function.mysql-result]: Unable to jump to row 1 on MySQL result index 21 in /home/ktalk/public_html/st/news.php on line 86

with the "on line 86" part going from 86-94, OVER AND OVER again right down the page  :'(

Rudolf

What are you trying to do?
The query you're using there will always return one row one value - the number of rows in the table.
Also you have two calls to mysql_query, where the first one is absolutely useless.

mysql_query($query);
$result=mysql_query($query);

Looking at your code I think what you want is to first get the number of rows then the actual data from the table. You are not doing that though.
The result set from the query does not have the columns you are referencing in the mysql_result functions. Thus, it fails.

This part is really messed up.

$i=$num;
while ($i > 0) {

$newsid=mysql_result($result,$i,"newsid");
$newstitle=mysql_result($result,$i,"newstitle");
$newscontent=mysql_result($result,$i,"newscontent");
$newsauthor=mysql_result($result,$i,"newsauthor");
$newsmonth=mysql_result($result,$i,"newsmonth");
$newsday=mysql_result($result,$i,"newsday");
$newsyear=mysql_result($result,$i,"newsyear");
$newscontentprvw=mysql_result($result,$i,"newscontentprvw");
$newsshowarticle=mysql_result($result,$i,"newsshowarticle");
$newscontentprvw.='...';


What you want is probably something like this

$query="SELECT * FROM news WHERE newshowarticle = 1";
$result=mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
echo '<tr>
          <td valign="top" class="mytext2"><strong>', $row['newstitle'], '</strong><span class="mytext"><br />
            ', $row['newscontentprvw'], '</span><br />
            <img src="images/bul.gif" width="4" height="6" /> <a href="?act=news&id=', $row['newsid'], '">Read More</a></td>
        </tr>';
}

These 9 lines of codes might do just what you want, depending if I understood correctly what you want.

I couldn't figure out what for are those $a and $i.
I will update all my mods in the next few weeks. Thanks for your patience.

SVG-Collapse (you need an SVG compliant browser)

kL

#3
the while loop is supposed to get the two latest news articles and echo them in a table
the two latest would be the last two rows so i made it so $i = the number of rows so it starts from the last row and gets the stuff from that row then $i == $i - 1 so it goes to the row before it. and when $a = 2 it makes $i = 0 so that it stops because i wanted two. i know i did it wrong though. thanks for your help ill try that.

fwitt

#4
-edit- this posts comes accross as a bit harsh when i read it back, that wasnt the intention -/edit-



$a=1;
$i=$num;
while ($i > 0) {

do some stuff

$a++;

if ($a == 2 ) {$i = 0; }

$i = $i - 1;
}}



this while loop implies to me you were having a bad day in the thinking department,
lets first look at variable $a, you set it equal to one, next time its seen it is increased by one to two, which means it makes the if statement true so it resets $i to zero, which makes $i -1 at the end of the loop breaking the while statement and hence the loop. this code is in fact a very obfuscated way of doing this simple if loop



$i = $num
if ($i > 0) {

do some stuff

}



if you wanted to use $a as a counter why not put it in the while statement rather than forcing $i to do 2 jobs



$a=0;
$i=$num;
while ($a<2) {

do some stuff

$a++;
$i = $i - 1;
}}



basically i think you got yourself in a muddle and kept going.

kL

thank you ive sorted it out now, im sort of new to php and mysql. i guess i should rtfm  :-[

fwitt


Advertisement: