I have no idea if I am doing this correctly. Eight years ago I was a C++ programmer, then I left the industry. I am lost and have been reading heaps on the web but do not know where to begin.

I wrote a MySQL DB, I have populated the DB using a PHP web page. I have put about 400 records in the DB.

I can also connect to the DB using PHP and write all the record to a web page.

My problem is I want to place one record at a time on the page. It is to help my son memorize many quotes he needs for school. I would like to be able to show one record, then when that is memorized or recalled which ever my son does. I would like to use AJAX to write the next record on the page, not appending to the last but over write the last.

Is there a way to put the PHP array into a JS array? I know I can do this.

<?
/*
 *  php code for loading an array to be used to load 
 *  a JavaScript array, that will be printed to 
 *  document. 
*/		
		for($b=0;$b<181;$b++)
		  {
		  	$a[]=$b;//loading the array '$a' using php
		  }
 ?>

<script language="javascript" type="text/javascript">
/*
 *  Function 
 *  before:
 * 			php array filled above
 * 			js array not alive
 * 			
 *  after:
 * 			js array alive
 * 			js array populated
 * 			js array written to document
 */
	function showValues()
         {
		var a=new Array;//new array
		//next line is open PHP script in JavaScript function
	     <?
		for($i=0;$i<count($a); $i++)
		{
			echo "a[$i]='".$a[$i]."';\n";
		}			
	     ?>
	 
                //above line closes php script
	 
	 document.write("This is a JavaScript array loaded from a PHP array then  printed to document!<br/><br/>")

		for(i=0;i<a.length;i++)
		  {
			document.write(a[i]+' ');
			 if((i>0)&&(i%30==0))
			  {
			  	document.write('<br/>');
			  }
		  }		
	}
	 </script>

But I am not even certain if this is the most intelligent way of doing this or if there is a smoother more intelligent way.

Is there a way that is the industry standard for capturing all the records from a MySQL DB and then using AJAX writing them on the web page as the client needs. I mean if the client only wants the first record then that is where they quit but if they read the first, second, then third and then forth they click to get to the next or previous record?

I think loading all records in client is not good way of doing it when number of records are more then in tens.
So ajax-jquery combination is best way

you can learn jquery/ajax at

http://phpacademy.org/videos/index.php?all

the function you are asking for can be achieved by using a FOR loop in php, and let it echo a piece of javascript every time. like this:

<script type="text/JavaScript" lang="JavaScript">
var $array[];
<?php
 $sql = "SELECT * FROM blah";
$exec = mysql_query($sql);

$loop = 1;
for($row = mysql_fetch_array($exec))
{
  echo ("araay[" . $loop . "] = " . $row['fieldtobeechoed'] . ";");
  $loop++;
}
?>

No, this isn't efficient and i'd also suggest using a AJAX/JQuery approach.
But if you want to do this for small amounts of data, go ahead.

Oh, and i wrote this out of the top of my head, might contain typos

I think loading all records in client is not good way of doing it when number of records are more then in tens.
So ajax-jquery combination is best way

you can learn jquery/ajax at

http://phpacademy.org/videos/index.php?all

I have been on phpacademy, thanks for pointing that URL out; I think I have watched most of the tutorials they are well done.

My problem is that I trying to get all the records from the MySQL DB, which is really easy. Then I want to print only 1 record on the page at a time, meaning only 1 record can be seen at a time. I do not want to loop through the entire set of records as that would print them all onto the page at once.


I have tried

$myGlobal=0;

if(isset($_POST['title'],$_POST['post']))
 {
	global $myGlobal;
	mysql_data_seek($result,$myGlobal);
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	echo $row['id'].' == id<br/>';
        echo $row['title'].' == title<br/>';
	echo $row['contents'].'== contents<br/>';
	echo $myGlobal++.'<br/>';
 }

<form method="post" action="">
<div>
<label for='title'>Title:</label>	
<input type="text" name="title" id="title"/>
</div>
<div>
<label for='post'>Post:</label>	
<textarea name="post" id="post" rows="15" cols="50"></textarea>
</div>
<div id="printHere"></div>
<div>
<br/>
<input type="submit" value="Post" />
</div>
</form>

but this will only show the 1st record of the recordset. I cannot seem to get the internal pointer to move one ahead.

the function you are asking for can be achieved by using a FOR loop in php, and let it echo a piece of javascript every time. like this:

<script type="text/JavaScript" lang="JavaScript">
var $array[];
<?php
 $sql = "SELECT * FROM blah";
$exec = mysql_query($sql);

$loop = 1;
for($row = mysql_fetch_array($exec))
{
  echo ("araay[" . $loop . "] = " . $row['fieldtobeechoed'] . ";");
  $loop++;
}
?>

No, this isn't efficient and i'd also suggest using a AJAX/JQuery approach.
But if you want to do this for small amounts of data, go ahead.

Oh, and i wrote this out of the top of my head, might contain typos

Thanks for this idea, I tried it and it does print the entire set of records.

My problem is I want to print only 1 record on the page at a time. When the person viewing the record wants the next record I want only the 'next' record visible on the web page. Then the next and then the next/ or the previous.

I have tied many different ideas but am totally lost on this.

Thanks again for your help.

well, if you want to select a specific amount of rows, you can use LIMIT in your sql tag, as in

SELECT * FROM blah LIMIT 1,1

.
the first 1 is the lowest record it should pick, 1 in this case, and the second value is the highest case. you could then create a "forward" button or so, which refers to your same page, but then with a GET value (like, 2) and replace the two ones in your sql code with the values of the GET.

example:

if (!isset($_GET['row']))
{   $_GET['row'] = 1;   }
// if this is your first time visting the page, you might not have a get value. thus,
// if it is not set, set it manually :)

$sql = "SELECT * FROM blah LIMIT " . $_GET['row'] . ", " . $_GET['row'];
$exec = mysql_query($sql);

$newrow = $_GET['row'] + 1;
//we'll create a new variable that'll hold the number for our next GET link

echo("<form method='GET' action='thispage.php?row=" . $newrow . "'");

// put the rest of your code here. a for loop or while loop won't be necessary if it's
// just one row.

once again, it's just out of the top of my head, it'll be buggy :)

Great thanks for this help, I will get after this today. This is heaps of help thank.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.