• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 693
  • Last Modified:

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

0
James_Avery
Asked:
James_Avery
  • 8
  • 7
1 Solution
 
Dave BaldwinFixer of ProblemsCommented:
Where are you getting the number after the name form?
0
 
James_AveryAuthor Commented:
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?


0
 
Dave BaldwinFixer of ProblemsCommented:
That does but you started with '1' in your example.  If I understand, this should do what you want.
flippingBook.contents = [
	<?php 
        $count = 0;
        while ($row_projects = mysql_fetch_assoc($projects)) { 
        $count = $count+2; ?>

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

        <?php } ; ?>
];

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
James_AveryAuthor Commented:
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?
0
 
Dave BaldwinFixer of ProblemsCommented:
Was the code you posted displaying anything?  Are you opening the database and getting the table info first?
0
 
James_AveryAuthor Commented:
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

0
 
James_AveryAuthor Commented:
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.
0
 
Dave BaldwinFixer of ProblemsCommented:
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.
0
 
Dave BaldwinFixer of ProblemsCommented:
"$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.
0
 
James_AveryAuthor Commented:
Oh, how silly of me.  After modifying the code by adding the query again, your solution worked.  Thanks!
0
 
Dave BaldwinFixer of ProblemsCommented:
You're welcome.  It's hard to review our own code at times!
0
 
James_AveryAuthor Commented:
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  ]; ?
0
 
Dave BaldwinFixer of ProblemsCommented:
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

0
 
James_AveryAuthor Commented:
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?
0
 
Dave BaldwinFixer of ProblemsCommented:
No.  The display $count should start with '2' because that's what you asked for.  The row should be the first row.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now