Link to home
Start Free TrialLog in
Avatar of James_Avery
James_Avery

asked on

Mysql PHP repeat region format help

I need a PHP repeat region for the code below. I already have the repeat region set up, but I need some additional code added.

I need to replace each line under 'flipping.Book.contents' with a field from my database called 'name'.  The number to the right of the echo show below is a page number, which I  need to increase by two with each loop.

The last line of the loop should NOT contain a comma at the end, as seen in the example below.

Any ideas?


flippingBook.contents = [
	[ "Select Publication", 1 ],

	[ "D Home - Mar-Apr 2009", 2 ],

	[ "D Home - Feb 2009", 4 ]
];

Open in new window

// THE WAY THE JAVASCRIPT CODE LOOKS NOW

flippingBook.contents = [
	<?php do { ?>

        [ "<?php echo $row_projects['name']; ?>", 1 ],

        <?php } while ($row_projects = mysql_fetch_assoc($projects)); ?>
];


// THE WAY IT NEEDS TO LOOK AFTER PROCESSING PHP

flippingBook.contents = [
	[ "Name 1", 1 ],

	[ "Name 2", 2 ],

	[ "Name 3", 4 ],
        
        [ "Name 4", 6 ],
         
        [ "Name 5", 8 ],
        
        [ "Name 6", 10 ],
        
        [ "Name 7", 12 ]
];

Open in new window

Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Where are you getting the number after the name form?
Avatar of James_Avery
James_Avery

ASKER

The number doesn't come from any source. It should be automatically generated by the PHP script by simply added 2 to whatever the number in the previous loop was.

It should start with '2', then '4', '6' '8' etc... does that make sense?


ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hmmm, this doesn't seem to be creating a repeat region.  When run, it just comes up empty.  I am not seeing any errors associated with that piece of mysql code either.

Any ideas?
Was the code you posted displaying anything?  Are you opening the database and getting the table info first?
Below is my code that I am working with.  For some reason anything following my first repeat region is coming up blank.  Is it because I have another repeat region?
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$query_projects = "SELECT * FROM projects WHERE activeflag = '2' AND layout = '1' ORDER BY dateadded DESC";
$projects = mysql_query($query_projects, $con) or die(mysql_error());
$row_projects = mysql_fetch_assoc($projects);
$totalRows_projects = mysql_num_rows($projects);
?>
	
flippingBook.pages = [

	"../../../images/press/print-ads/d-home-2009-03-04/02.jpg",
	<?php do { ?>
    "../../../images<?php echo $row_projects['imagepath']; ?>hero_a.jpg",    
    "../../../images<?php echo $row_projects['imagepath']; ?>hero_b.jpg",  
    <?php } while ($row_projects = mysql_fetch_assoc($projects)); ?>
	"../../../images/press/print-ads/d-home-2009-03-04/02.jpg"
    ];


flippingBook.contents = [
	<?php 
        $count = 0;
        while ($row_projects = mysql_fetch_assoc($projects)) { 
        $count = $count+2; ?>

        [ "<?php echo $row_projects['name']; ?>", <?php echo $count; ?> ],

        <?php } ; ?>
	[ "D Home - Feb 2009", 4 ]
];

Open in new window

I establish the connection to the DB above this code. My first repeat region works great, but the set is only printing my javascript and not the info from the database, as if the PHP is outputting nothing.
That's because you've already 'used' the info from that database in the previous section.  You can either run the query again or store the info you get into an array which you can run thru twice.
"$row_projects = mysql_fetch_assoc($projects)" increment an array pointer each time you call it.  "mysql_data_seek($projects, 0)" might reset the pointer so you can go thru the data again.  I've never used it myself so I'm not sure.
Oh, how silly of me.  After modifying the code by adding the query again, your solution worked.  Thanks!
You're welcome.  It's hard to review our own code at times!
One last question.. I noticed your code retains the comma in the last loop of the repeat region.  On the last line, can we automatically remove the comma at the end before the  ]; ?
I think this will do it.
flippingBook.contents = [
	<?php 
        $count = 0;
        while ($row_projects = mysql_fetch_assoc($projects)) { 
        if($count) echo ",";
        $count = $count+2; ?>

        [ "<?php echo $row_projects['name']; ?>", <?php echo $count; ?> ]

        <?php } ; ?>
];

Open in new window

I just noticed that when using your code, I am losing the first record in the recordset.  The first record to appear is actually the second record in the table, not the first record.  The recordset I am using above shows the correct first record, but your recordset I added jumps to the 2nd record and starts from there.

Any idea why your code is shaving off the first record in the recordset and starting with the 2nd?
No.  The display $count should start with '2' because that's what you asked for.  The row should be the first row.