Jump to content

DarkJamie

Members
  • Posts

    27
  • Joined

  • Last visited

    Never

Posts posted by DarkJamie

  1. I've solved the problem. For anyone looking to display "x" amount of characters of a query result, the following function works perfectly:

     

    Define your $text variable from your query results (in my case "content")

    $text=$row['content'];
    

     

    Set the number of characters by defining $length and the trailing characters (...) by defining $tail

    function snippet($text,$length=164,$tail="...") { 
    $text = trim($text); 
    $txtl = strlen($text); 
    	if($txtl > $length) { 
    		for($i=1;$text[$length-$i]!=" ";$i++) { 
                			if($i == $length) { 
    				return substr($text,0,$length) . $tail; 
               			} 
           			} 
            		$text = substr($text,0,$length-$i+1) . $tail; 
        		} 
        		return $text; 
    }
    

     

    then to print the limited result:

    echo snippet($text);
    

     

  2. I am having trouble finding info on how to display a partial result from my sql query. I know what I'd like to do is really simple, but my search always results in how to paginate my query results rather than display the partial data with more link. I'd like it to display like this:

     

    Story Title - Date Entered

    words go here words go here words go here words go here words go here words go here words go here words go here words go here words go here words go here words go here words go here words go here words go here words go here words go here ...more

     

    I can display my results just fine, I would just like to place the partial story on my main page like this. If anyone can point me in the right direction so I can read up on it, I'd be greatful.

  3. I see where you're going with that, but before the version change, I didn't need to set the $id value for it to work. I'm still trying to wrap my brain around that logic, but it worked fine. I was able to add entries, edit them, delete them, and list them in any fashion i wanted with just the code I had. Now I'm sure the version change shouldn't have made a difference, since nothing on my site is actually dependent on either version yet, but once I implemented the change, the add_entry, edit_entry, delete_entry and results by id all stopped working.

     

    Having given thought to this logic, I went back and switched BACK to php4 and everything is working again, so I guess my question should be, with something as simple as a query by ID, how will I need to rework the code to work in php5?

  4. I'm hoping there is a simple solution for this problem. Yesterday I upgraded from PHP4 to PHP 5, as I want to use ImageMagick for my photo gallery. Now that I've upgraded, my blog queries only partially work. The following are two snippets of code querying the same db.

     

    This is my list page. It displays a list of titles with entry dates. This page works perfectly, and displays everything correctly.

    <?php
    //Generate the query
    
    $query = "SELECT id, title, content, author, UNIX_TIMESTAMP(entry_date) as date FROM news ORDER BY date DESC LIMIT 3";
    $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
    
    // get resultset as object
    if (mysql_num_rows($result) > 0) {
    	while($send = mysql_fetch_object($result)) {
    
    	// print details
    
    	echo "<a href='r.php?id=$send->id'><b class='style2'>$send->title</b></a> ";
    
    		?>
    		<font class="style3">
    		<?php
    		echo date("g:i A - F j, Y", $send->date +7200);
    		?>
    		</font><br>
    		<?php
    
    	echo "<font class='style7'><i>Written by: </i><b>$send->author</b> </font><br>";
    	echo "<br>";
    	echo nl2br($send->content);
    	echo "<br><br>";
    
    	}
    }
    ?> 

     

    This is my results page. When executed, this page returns no result and no error messages.

    <?php
         include("includes/dark_db_access.php");
           db_login();
    
    	//Generate the query to display news content
    	$query = "SELECT title, content, author, UNIX_TIMESTAMP(entry_date) as date FROM news WHERE id = '$id'";
    	$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
    
    	// get resultset as object
    	$send = mysql_fetch_object($result);
    
    	// print details
    	if($send) {
    		echo "<b class='style2'>$send->title</b> ";
    ?>
    
    				<font class="style3">
    				<?php
    				echo date("g:i A - F j, Y", $send->date +7200);
    				?>
    				</font>
    				<?php
    				echo "<br><i>Written by: </i><b>$send->author</b> ";
    				echo "<br><br>";
    				echo nl2br($send->content);
    				echo "<br><br><font size=2 color=red><a href=my_sent.php>Main menu.</a></font>";
    				}
    				?>

     

    Since both queries are written in the same syntax, and the php code to display them is the same, I can not figure out why the results page (by id) fails to display anything. Unfortunately, since I'm hosted with GoDaddy, their geniuses, in their infinite wisdom, thought it best that economy users not have access to error logs, so I can't check them. Altho my brain's logic keeps telling me that if the list page works, and the results page is written the same way, it should work too. Mind you, until I switched to php5 yesterday, all of my db queries were working perfectly.

     

    Any ideas would be greatly appreciated.

  5. progress. Now I'm getting the unformatted UNIX timestamp, but when I changed the line to:

    	echo $send->date("g:i A - F j, Y");

     

    it returned Fatal error: Call to undefined function: date() in /r.php on line 204

     

    so I'm not sure how to get the formatting right calling from the $send array.

  6. Thank you for your help in this. In order to be more clear with my naming convention, I have changed the "date" table name to "entry_date" in the db to avoid a conflict with the date function.

     

    Taking your recommendation,  I've removed the included site_funct with it's date function and used UNIX_TIMESTAMP in my SQL call.

     

    Now I'm getting "0" (literally zero) rather than the entry date. 

     

    No errors are generated

     

    My code is as follows:

     

    SQL table"

    `entry_date` datetime NOT NULL default '00-00-0000 00:00:00',

     

    My display PHP code:

    <?
    //Generate the query to display news content
    $query = "SELECT title, author, content AND UNIX_TIMESTAMP(entry_date) as date FROM news WHERE id = '$id'";
    $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
    
    // get resultset as object
    $send = mysql_fetch_object($result);
    
    // print details
    if($send) {
    echo "<b class='style2'>$send->title</b> ";
    
    echo $send->date;
    
    echo "<br><i>Written by: </i><b>$send->author</b> ";
    
    echo "<br><br>";
    
    echo nl2br($send->content);
    
    echo "<br><br><font size=2 color=red><a href=my_sent.php>Main menu.</a></font>";
    }
    ?> 
    

     

    Here is the SQL from the entry page:

    $query = "INSERT INTO news(id, title, content, author, entry_date) VALUES(0, '$title', '$content', '$author', NOW())";
    

  7. I have written a personal blog site based on a tutorial for such and it works great, except for one thing. The tutorial was written to store the date of each post in the db and return the month, day and year. It works how it is supposed to, but I would like to display the time of each post as well. Here is where it gets messed up. I suspect the writer of the tutorial left out the time due to this very problem. The date is being set properly, but the time for each entry is the same, which obviously can't be.

     

    This is the code that the tutorial provided for the function:

    function formatDate($val) {
       $arr = explode("-", $val);
       return date("d M Y", mktime(0,0,0, $arr[1], $arr[2], $arr[0]));
    }

     

    Here is the change that I made for formatting:

    function formatDate($val) {
    $arr = explode("-", $val);
    return date("g:i A - F j, Y", mktime($arr[1], $arr[1], $arr[1], $arr[1], $arr[2], $arr[0]));
    }
    

     

    the function is called upon like this:

    echo formatDate($send->date);
    

     

    the mysql table was set like this:

      `date` datetime NOT NULL default '00-00-0000 00:00:00',

     

    I have changed the numbers in each instance of $arr in the above function to see how they would effect the results. Regardless of what values I put in, the times would change, but each entry would display the same time, rather than their actual post times. (all entries posted at 2:02 AM - February 10, 2010)

     

    I could just go back to the default setting to display only the day/month/year, but as I learn more PHP, I want to use the date and time values to display a calendar picture with sprites rather than a textual time stamp.

     

    So my question is, what would be the best way to properly display the timestamp for each entry?

     

     

     

  8. Hi folks. I'm back with another question on displaying array results. My problem is pretty simple, I just haven't been successful in executing it. I have a page that displays a list of products with checkboxes and prices. The checked items go into an array called "reports". The data is then passed to the selection page, where I wish to display each product description and price based on the ID values from the checkbox array.

     

    So what I am asking is how do I display the description and price of each product in the array (and then print a sum of their prices)?

     

    here is my code for the selection page. It is incomplets, as I have cleaned out my erroneous code.

     

    if (isset($_POST['reports']))
    {
    	foreach ($_POST['reports'] as $key => $rid)
    
    
    echo "<h2>Individual Reports</h2>";
    echo "<h4>You've selected the following reports:</h4>";
    
    
    	while ($row=mysql_fetch_array($sql_result)) {
    	    $id = $row["id"];
        	    $file_number=$row["file_number"];
          	    $filename=$row["filename"];
          	    $description=$row["description"];
    	    $price=$row["price"];
    	    $active=$row["active"];
    
    
    		echo "<table border=0>";
    		echo "<tr>";
    		echo "<td width=320px>$description</td>";
    		echo "<td><b>$price</b></td>";
    		echo "</tr>";
    		echo "</table>";
    
    	}
      
      echo "</table>"; 
    
    }

  9. I've simplified the actual checkbox line in my table and fixed the syntax error of the missing >.

     

    report_list.php code:

     

    <?php
    $path = $_SERVER['DOCUMENT_ROOT'];
    
    include ($path.'/incs/dbsetup.php');
    
    ini_set('display_errors',1);
    error_reporting(E_ALL);
    
    
    $connect=mysql_connect ($server,$user,$pword) or die ("Could Not connect to MySQL Server");
    
        $db=mysql_select_db ($dbname,$connect) or die ("Could not select MySQL Database");
    
      		$sql="select * from downloads order by file_number";
    
    	$sql_result=mysql_query($sql,$connect) or die ("Could Not Query Database");
    
    	$num=mysql_numrows($sql_result);
    
    		if($num == 0) {
    
    	echo "<center> Sorry! there were no Files that matched your search. </center>";
        
    	}
      		
    
    
    	echo "<h2>Select the reports you would like to purchase</h2>";
    	echo "<br>";
    
    
    	echo "<form action='file_select.php' method='POST'>";
    
    
    
    	while ($row=mysql_fetch_array($sql_result)) {
    			$id = $row["id"];
        	    $file_number=$row["file_number"];
          		$filename=$row["filename"];
          		$description=$row["description"];
    			$price=$row["price"];
    			$active=$row["active"];
    			        
    
    		echo "<table border=0>";
    		echo "<tr>";
    		echo "<td width=320px><input type='checkbox' name='item[]' value='$id'> $description</td>";
    		echo "<td><b>$price</b></td>";
    		echo "</tr>";
    		echo "</table>";
    
    	}
    
    	echo "<br><input type='submit' name='submit' class='button' value='Continue' tabindex='2'>"; 
    
    
    //		
    	echo "</form>";
    	echo "<br>";
    ?>
    

     

  10. Here's the report_list.php code:

     

    <?php
    $path = $_SERVER['DOCUMENT_ROOT'];
    
    include ($path.'/incs/dbsetup.php');
    
    ini_set('display_errors',1);
    error_reporting(E_ALL);
    
    
    $connect=mysql_connect ($server,$user,$pword) or die ("Could Not connect to MySQL Server");
    
        $db=mysql_select_db ($dbname,$connect) or die ("Could not select MySQL Database");
    
      		$sql="select * from downloads order by file_number";
    
    	$sql_result=mysql_query($sql,$connect) or die ("Could Not Query Database");
    
    	$num=mysql_numrows($sql_result);
    
    		if($num == 0) {
    
    	echo "<center> Sorry! there were no Files that matched your search. </center>";
        
    	}
      		
    
    
    	echo "<h2>Select the reports you would like to purchase</h2>";
    	echo "<br>";
    
    
    	echo "<form action='file_select.php' method='POST'>";
    
    
    
    	while ($row=mysql_fetch_array($sql_result)) {
    			$id = $row["id"];
        	    $file_number=$row["file_number"];
          		$filename=$row["filename"];
          		$description=$row["description"];
    			$price=$row["price"];
    			$active=$row["active"];
    			        
    
    		echo "<table border=0>";
    		echo "<tr>";
    		echo "<td width=20px><input type='checkbox' name='item[]' value='$id'</td>";
    		echo "<td width=300px >$description</td>";
    		echo "<td><b>$price</b></td>";
    		echo "</tr>";
    		echo "</table>";
    
    	}
    
    	echo "<br><input type='submit' name='submit' class='button' value='Continue' tabindex='2'>"; 
    
    
    //		
    	echo "</form>";
    	echo "<br>";
    ?>
    

     

  11. I've added the pre print code as suggested and that line now errors before it gets to the previous error:

     

    arse error, unexpected T_CONSTANT_ENCAPSED_STRING in /html/file_select.php on line 8, referer: /report_list.php

     

    Just to be certain, here is the code from the report_list.php file:

     

    <?php
    $path = $_SERVER['DOCUMENT_ROOT'];
    include ($path.'/incs/dbsetup.php');
    
    ini_set('display_errors',1);
    error_reporting(E_ALL);
    
    echo '<pre>'.print_r($_POST,true).'</pre>';
    
    Echo "<h2>Individual Reports</h2>";
    
    if (isset($_POST['item'])) {
    echo 'You selected '.count($_POST['item']).' items<br/>';
    foreach ($_POST['item'] as $value) {
    	echo $value . ' ';
    }
    } else {
    echo 'Nothing Selected';
    }
    
    
    ?>
    

     

  12. Thanks Buddski,

     

    I was just reading up on isset when your post came through. I changed the code as you suggested, but still get no results. Now instead of a T_VARIABLE error, I get a T_STRING error. Here is the error log:

     

    parse error, unexpected T_STRING in /html/file_select.php on line 11, referer: /html/report_list.php

     

    As with both parsing errors, I've checked semicolons and bracets and such, but can't seem to find what's wrong.

  13. Here is the code for file_select.php

     

    <?php
    $path = $_SERVER['DOCUMENT_ROOT'];
    include ($path.'/incs/dbsetup.php');
    
    ini_set('display_errors',1);
    error_reporting(E_ALL);
    
    
    Echo "<h2>Individual Reports</h2>";
    
      $aReport = $_POST['item'];
      if(empty($aReport)) 
      {
        echo("You didn't select any reports.");
      } 
      else 
      {
        $N = count($aReport);
    
        echo("You selected $N report(s): ");
        for($i=0; $i < $N; $i++)
        {
          echo($aReport[$i] . " ");
        }
      }
    
    
    ?>
    

     

  14. I will have to get back to you on apache logs. For now, here is the complete code for the report_list.php:

    <?php
    $path = $_SERVER['DOCUMENT_ROOT'];
    
    include ($path.'/incs/dbsetup.php');
    
    ini_set('display_errors',1);
    error_reporting(E_ALL);
    
    
    $connect=mysql_connect ($server,$user,$pword) or die ("Could Not connect to MySQL Server");
    
        $db=mysql_select_db ($dbname,$connect) or die ("Could not select MySQL Database");
    
      		$sql="select * from downloads order by file_number";
    
    	$sql_result=mysql_query($sql,$connect) or die ("Could Not Query Database");
    
    	$num=mysql_numrows($sql_result);
    
    		if($num == 0) {
    
    	echo "<center> Sorry! there were no Files that matched your search. </center>";
        
    	}
      		
    
    
    	echo "<h2>Select the reports you would like to purchase</h2>";
    	echo "<br>";
    
    
    	echo "<form action='file_select.php' method='POST'>";
    
    
    
    	while ($row=mysql_fetch_array($sql_result)) {
    			$id = $row["id"];
        	    $file_number=$row["file_number"];
          		$filename=$row["filename"];
          		$description=$row["description"];
    			$price=$row["price"];
    			$active=$row["active"];
    			        
    
    		echo "<table border=0>";
    		echo "<tr>";
    		echo "<td width=10px><input type='checkbox' name='item[]' value=$id</td>";
    		echo "<td width=300px>$description</td>";
    		echo "<td><b>$price</b></td>";
    		echo "</tr>";
    		echo "</table>";
    
    	}
    
    	echo "<br><input type='submit' class='button' value='Continue' tabindex='2'>"; 
    
    
    //		
    	echo "</form>";
    	echo "<br>";
    ?>
    
    

     

    The dbsetup.php is fine, as the site has been up for a couple of years with all other functionality working ok.

     

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