PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > macromedia.dyna.html > Converting cm to inches in a line of text
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Converting cm to inches in a line of text

Réponse
 
LinkBack Outils de la discussion
Vieux 01/10/2008, 17h46   #1
colinwalton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Converting cm to inches in a line of text

I would like to convert to inches the numbers immediately preceding 'cm' in the
dynamic dimensions text.

length 120cm depth 30cm height 70cm

So want to dynamically do this.... length (120x0.394)in depth (30x0.394)in
height (70x0.394)in

Rounded to 1 decimal point to get...

length 47.38in depth 11.9cm height 27.6cm

I think its something to do with explode and implode but haven't a clue where
to start! There are 300 entries that I need to output in inches as well as cm,
so its worth doing the computation in PHP rather than calculating each one by
hand...

Can anyone with exact PHP code I need please?

  Réponse avec citation
Vieux 01/10/2008, 23h55   #2
David Powers
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Converting cm to inches in a line of text

colinwalton wrote:
> I would like to convert to inches the numbers immediately preceding 'cm' in the
> dynamic dimensions text.
>
> length 120cm depth 30cm height 70cm
>
> So want to dynamically do this.... length (120x0.394)in depth (30x0.394)in
> height (70x0.394)in
>
> Rounded to 1 decimal point to get...


Create the following function and pass each value to it:

function cmToIn($val) {
return round($val * .394, 1).'in';
}

For example:

$len = '120cm';
echo cmToIn($len); // displays 47.3in.

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
  Réponse avec citation
Vieux 02/10/2008, 09h31   #3
colinwalton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Converting cm to inches in a line of text

Hi thanks for the reply...

I understand where to put the function, but don't understand what I need to do
to use the chunk of script.

This is the code for the existing cm - can you possibly rework it so I can see
what I need to do for the equivalent inches?

<?php do { ?>
<?php echo $row_getsDimensions_RS['dims']; ?> code <?php echo
$row_getsDimensions_RS['code']; ?><br>
<?php } while ($row_getsDimensions_RS =
mysql_fetch_assoc($getsDimensions_RS)); ?>


THANKS!

C

  Réponse avec citation
Vieux 02/10/2008, 11h52   #4
David Powers
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Converting cm to inches in a line of text

colinwalton wrote:
> This is the code for the existing cm - can you possibly rework it so I can see
> what I need to do for the equivalent inches?
>
> <?php do { ?>
> <?php echo $row_getsDimensions_RS['dims']; ?> code <?php echo
> $row_getsDimensions_RS['code']; ?><br>
> <?php } while ($row_getsDimensions_RS =
> mysql_fetch_assoc($getsDimensions_RS)); ?>


Looking at the code you have given here, it seems as though you are
storing "length 120cm depth 30cm height 70cm" as a single string in the
dims column of the database. If so, that's totally crazy. You should
have three separate columns, one each for length, depth, and height. You
should also store the dimensions as numbers. You could then use SQL to
perform the calculation at the same time as retrieving the data.

Storing the dimensions as a string is prone to mistakes, time-consuming,
and slows down the processing of the results. The following code does
what I think you want to do, but I don't recommend using it. You would
be much better off redesigning your database.

function cmToIn($val) {
return round($val * .394, 1).'in';
}
function convertDimensions($dims) {
$pattern =
'/length\s+(\d+)\s?cm\s+depth\s+(\d+)\s?cm\s+height\ s+(\d+)\s?cm/i';
if (preg_match($pattern, $dims, $m)) {
return 'length '.cmToIn($m[1]).' depth '. cmToIn($m[2]).' height
'.cmToIn($m[3]);
} else {
return 'Cannot convert dimensions';
}
}


<?php do { ?>
<?php echo convertDimensions($row_getsDimensions_RS['dims']); ?> code
<?php echo $row_getsDimensions_RS['code']; ?><br>
<?php } while ($row_getsDimensions_RS =
mysql_fetch_assoc($getsDimensions_RS)); ?>

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
  Réponse avec citation
Vieux 02/10/2008, 12h37   #5
colinwalton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Converting cm to inches in a line of text

Hi,

Thanks for this - I am really grateful for your time. Sadly even with this
precise code the blasted inches don't display - I just get the Cannot convert
dimensions message.

The trouble is that I am updating someone else's database so have been given
the job of trying to make work the material I already have.

Can you possibly explain this line of code so I might be able to understand
what may be going wrong?

'/length\s+(\d+)\s?cm\s+depth\s+(\d+)\s?cm\s+height\ s+(\d+)\s?cm/i';

what is the reason for the / and \
what does length\s mean? does this allow for lengthS ?
what does (\d+) do?
what does this ? do? is this where to put the answer?

I am sure its back to the very basics for you - I am off to buy your book at
lunch time!

Thanks for your time

C

  Réponse avec citation
Vieux 02/10/2008, 14h12   #6
Michael Fesser
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Converting cm to inches in a line of text

..oO(David Powers)

>colinwalton wrote:
>> This is the code for the existing cm - can you possibly rework it so I can see
>> what I need to do for the equivalent inches?
>>
>> <?php do { ?>
>> <?php echo $row_getsDimensions_RS['dims']; ?> code <?php echo
>> $row_getsDimensions_RS['code']; ?><br>
>> <?php } while ($row_getsDimensions_RS =
>> mysql_fetch_assoc($getsDimensions_RS)); ?>

>
>Looking at the code you have given here, it seems as though you are
>storing "length 120cm depth 30cm height 70cm" as a single string in the
>dims column of the database. If so, that's totally crazy. You should
>have three separate columns, one each for length, depth, and height. You
>should also store the dimensions as numbers. You could then use SQL to
>perform the calculation at the same time as retrieving the data.


Agreed.

>Storing the dimensions as a string is prone to mistakes, time-consuming,
>and slows down the processing of the results. The following code does
>what I think you want to do, but I don't recommend using it. You would
>be much better off redesigning your database.
>
>function cmToIn($val) {
> return round($val * .394, 1).'in';
>}
>function convertDimensions($dims) {
> $pattern =
>'/length\s+(\d+)\s?cm\s+depth\s+(\d+)\s?cm\s+height\ s+(\d+)\s?cm/i';
> if (preg_match($pattern, $dims, $m)) {
> return 'length '.cmToIn($m[1]).' depth '. cmToIn($m[2]).' height
>'.cmToIn($m[3]);
> } else {
> return 'Cannot convert dimensions';
> }
>}


This should be enough:

function convertDimensions($dims) {
return preg_replace_callback('/(\d+)cm/',
create_function('$v', 'return round($v[1] * .394, 1)."in";'),
$dims
);
}

Micha
  Réponse avec citation
Vieux 02/10/2008, 14h14   #7
David Powers
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Converting cm to inches in a line of text

colinwalton wrote:
> Can you possibly explain this line of code so I might be able to understand
> what may be going wrong?
>
> '/length\s+(\d+)\s?cm\s+depth\s+(\d+)\s?cm\s+height\ s+(\d+)\s?cm/i';


It's a Perl-compatible regular expression that searches for the
following pattern:

length
followed by \s+ (one or more spaces)
followed by \d+ (one or more numbers)
followed by \s? (an optional space)
followed by cm
followed by \s+ (one or more spaces)
followed by depth
followed by \s+ (one or more spaces)
followed by \d+ (one or more numbers)
followed by \s? (an optional space)
followed by cm
followed by \s+ (one or more spaces)
followed by height
followed by \s+ (one or more spaces)
followed by \d+ (one or more numbers)
followed by \s? (an optional space)
followed by cm

The i at the end makes it case-insensitive. The parentheses around \d+
capture the values of the numbers.

It works perfectly with the example you gave in your original post, but
it sounds as though the text is differently formatted. Regular
expressions are designed to do sophisticated pattern matching, but
without knowing what's in your database, it's impossible to craft a
regular expression that will do the job.

As I said before, storing the dimensions as a single string is
inefficient and prone to error. I know you didn't create the database,
but it sounds as though you have inherited a nightmare.

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
  Réponse avec citation
Vieux 02/10/2008, 16h07   #8
David Powers
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Converting cm to inches in a line of text

Michael Fesser wrote:
> This should be enough:
>
> function convertDimensions($dims) {
> return preg_replace_callback('/(\d+)cm/',
> create_function('$v', 'return round($v[1] * .394, 1)."in";'),
> $dims
> );
> }


Nice one. Much simpler. ;-)

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
  Réponse avec citation
Vieux 02/10/2008, 16h31   #9
colinwalton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Converting cm to inches in a line of text

You are a star - thank you for all this useful information - ordered your book
today...

In the mean time I have found that if I delete the line above with the
dimensions in cm the one in inches appears.

I thought I could repeat fields from recordsets anywhere?

I seem to have the problem that I can only have one or the other and not both.
I have tried copying the recordset and calling it something else then refering
the converting function to that recordset, to no avail.

This project is driving me mad. I took it on to me learn PHP and MySQL
but I think I have learnt I need to do something else instead!

Below is the code for the page from the recordset onwards if you bare to look
at it!
Oh and some of the products have diameter instead of width AAAAGGGHHHHHHH!!!!!!
----------------

$colname_rangeNameExistsPicYes = "-1";
if (isset($_GET['product'])) {
$colname_rangeNameExistsPicYes = $_GET['product'];
}
mysql_select_db($database_connectionCGP, $connectionCGP);
$query_rangeNameExistsPicYes = sprintf("SELECT rangeName FROM products WHERE
product = %s AND rangeName > '' AND Pic = 'Y'",
GetSQLValueString($colname_rangeNameExistsPicYes, "text"));
$rangeNameExistsPicYes = mysql_query($query_rangeNameExistsPicYes,
$connectionCGP) or die(mysql_error());
$row_rangeNameExistsPicYes = mysql_fetch_assoc($rangeNameExistsPicYes);
$totalRows_rangeNameExistsPicYes = mysql_num_rows($rangeNameExistsPicYes);

$colname_productsRSrangeOnly = "-1";
if (isset($_GET['cat'])) {
$colname_productsRSrangeOnly = $_GET['cat'];
}
$colname2_productsRSrangeOnly = "-1";
if (isset($_GET['product'])) {
$colname2_productsRSrangeOnly = $_GET['product'];
}
mysql_select_db($database_connectionCGP, $connectionCGP);
$query_productsRSrangeOnly = sprintf("SELECT products.rangeName FROM products
WHERE cat = %s AND product = %s",
GetSQLValueString($colname_productsRSrangeOnly,
"text"),GetSQLValueString($colname2_productsRSrang eOnly, "text"));
$productsRSrangeOnly = mysql_query($query_productsRSrangeOnly, $connectionCGP)
or die(mysql_error());
$row_productsRSrangeOnly = mysql_fetch_assoc($productsRSrangeOnly);
$totalRows_productsRSrangeOnly = mysql_num_rows($productsRSrangeOnly);

$colname_sendsRangeName_RS = "-1";
if (isset($_GET['cat'])) {
$colname_sendsRangeName_RS = $_GET['cat'];
}
$colname2_sendsRangeName_RS = "-1";
if (isset($_GET['product'])) {
$colname2_sendsRangeName_RS = $_GET['product'];
}
mysql_select_db($database_connectionCGP, $connectionCGP);
$query_sendsRangeName_RS = sprintf("SELECT * FROM products WHERE cat = %s AND
product = %s", GetSQLValueString($colname_sendsRangeName_RS,
"text"),GetSQLValueString($colname2_sendsRangeName _RS, "text"));
$sendsRangeName_RS = mysql_query($query_sendsRangeName_RS, $connectionCGP) or
die(mysql_error());
$row_sendsRangeName_RS = mysql_fetch_assoc($sendsRangeName_RS);
$totalRows_sendsRangeName_RS = mysql_num_rows($sendsRangeName_RS);

$colname_getsDimensions_RS = "-1";
if (isset($_GET['cat'])) {
$colname_getsDimensions_RS = $_GET['cat'];
}
$colname2_getsDimensions_RS = "-1";
if (isset($_GET['product'])) {
$colname2_getsDimensions_RS = $_GET['product'];
}
mysql_select_db($database_connectionCGP, $connectionCGP);
$query_getsDimensions_RS = sprintf("SELECT * FROM products WHERE cat = %s AND
product = %s ORDER BY dims DESC", GetSQLValueString($colname_getsDimensions_RS,
"text"),GetSQLValueString($colname2_getsDimensions _RS, "text"));
$getsDimensions_RS = mysql_query($query_getsDimensions_RS, $connectionCGP) or
die(mysql_error());
$row_getsDimensions_RS = mysql_fetch_assoc($getsDimensions_RS);
$totalRows_getsDimensions_RS = mysql_num_rows($getsDimensions_RS);

function cmToIn($val) {
return round($val * .394, 1).'in';
}
function convertDimensions($dims) {
$pattern =
'/width\s+(\d+)\s?cm\s+depth\s+(\d+)\s?cm\s+height\s +(\d+)\s?cm/i';
if (preg_match($pattern, $dims, $m)) {
return ' width '.cmToIn($m[1]).' depth '. cmToIn($m[2]).' height
'.cmToIn($m[3]);
} else {
return 'imperial dimensions not currently available';
}
}
?><html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Capital Garden Pots - <?php echo $row_productsRS['cat'];
?>&nbsp;-&nbsp;<?php echo $row_productsRS['product']; ?></title>

<link rel="StyleSheet" type="text/css" href="style.css">
</head>
<body>

<div id="middleProductDiv">
<table width="520" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center" valign="top"><a href="code.php?cat=<?php echo
$row_choosingProductRS['cat']; ?>"><img src="pics/<?php echo
$row_productsRS['code']; ?>.jpg" alt="<?php echo $product?>" title="click to
see more <?php echo $row_productsRS['cat']; ?>" border="0"></a></td>
</tr>
<tr>
<td align="center" valign="top"><p><br>
<span class="sectionTitle"><?php echo $row_productsRS['product'];
?></span><br>
<br />
<?php do { ?>
<?php echo $row_getsDimensions_RS['dims']; ?>&nbsp;<span
class="smallCaps">&nbsp;&nbsp;&nbsp;&nbsp;code</span>&nbsp;<?php echo
$row_getsDimensions_RS['code']; ?>
<br />
<?php } while ($row_getsDimensions_RS =
mysql_fetch_assoc($getsDimensions_RS)); ?>
<br />
<?php if ($totalRows_productsRS > 0) { // Show if recordset not
empty ?>
<?php echo $row_productsRS['add']; ?>
<?php } // Show if recordset not empty ?>
<br />
<br />


</td>
</tr>
<tr>
<td align="center" valign="top"><?php echo
convertDimensions($row_getsDimensions_RS['dims']); ?> <span
class="smallCaps">code</span><?php echo $row_getsDimensions_RS['code']; ?><br />
<br /></td>
</tr>
</table>

</div>
<div id="rangeRHtext">
<?php if ($totalRows_rangeNameExistsPicYes > 0) { // Show if recordset not
empty ?>
<table width="160" border="0" cellpadding="0" cellspacing="0"
id="tableToHide">
<tr>
<td><p class="sectionTitle"><a href="range.php?rangeName=<?php echo
$row_sendsRangeName_RS['rangeName']; ?>">SEE
ALL OF THE<br />
PRODUCTS IN THE<br />
<?php echo $row_sendsRangeName_RS['rangeName']; ?> RANGE</a></p>
</td>
</tr>
</table>
<?php } // Show if recordset not empty ?>
<table width="160" border="0" cellpadding="0" cellspacing="0">
<tr>
<td><p class="sectionTitle"><a href="code.php?cat=<?php echo
$row_choosingProductRS['cat']; ?>" class="sectionTitle">SEE MORE<br>
<?php echo $row_choosingProductRS['cat']; ?></a></p> </td>
</tr>


<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td><p><a href="contact.php" target="_self">For Bespoke Sizes<br>
Please contact us<br>
for a quotation.</a></p> </td>
</tr>
</table>
</div>

<? require_once("navigation.php"); ?>
<?php
mysql_free_result($productsRS);
mysql_free_result($choosingProductRS);
mysql_free_result($DimsOfProducsWITHOUTphotosRS);
mysql_free_result($rangeNameExistsPicYes);
mysql_free_result($productsRSrangeOnly);
mysql_free_result($sendsRangeName_RS);
mysql_free_result($getsDimensions_RS);
?>


  Réponse avec citation
Vieux 02/10/2008, 16h56   #10
colinwalton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Converting cm to inches in a line of text

THANKS - This works, HOORAH! but ONLY DISPLAYS if I delete the original
dimensions in cm.
I need to show the cm AND the inches, on the same page - darn it.
Is there perhaps something wrong with the recordset? (not that I would spot
anything if there were!)

$colname_getsDimensions_RS = "-1";
if (isset($_GET['cat'])) {
$colname_getsDimensions_RS = $_GET['cat'];
}
$colname2_getsDimensions_RS = "-1";
if (isset($_GET['product'])) {
$colname2_getsDimensions_RS = $_GET['product'];
}
mysql_select_db($database_connectionCGP, $connectionCGP);
$query_getsDimensions_RS = sprintf("SELECT * FROM products WHERE cat = %s AND
product = %s ORDER BY dims DESC", GetSQLValueString($colname_getsDimensions_RS,
"text"),GetSQLValueString($colname2_getsDimensions _RS, "text"));
$getsDimensions_RS = mysql_query($query_getsDimensions_RS, $connectionCGP) or
die(mysql_error());
$row_getsDimensions_RS = mysql_fetch_assoc($getsDimensions_RS);
$totalRows_getsDimensions_RS = mysql_num_rows($getsDimensions_RS);


  Réponse avec citation
Vieux 02/10/2008, 18h29   #11
David Powers
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Converting cm to inches in a line of text

colinwalton wrote:
> I need to show the cm AND the inches, on the same page - darn it.


If you are showing the metric and imperial measurements in the same
repeat region, it's simple:

Imperial measurements: <php echo $row_getsDimensions_RS['dims']; ?><br />
Metric measurements: <php echo
convertDimensions($row_getsDimensions_RS['dims']); ?>

If you want to display the results in different repeat regions, you need
to rewind the recordset like this:

<?php
mysql_data_seek($getsDimensions_RS, 0);
$row_getsDimensions_RS = mysql_fetch_assoc($getsDimensions_RS);
do {
// reuse the recordset here
} while ($row_getsDimensions_RS = mysql_fetch_assoc($getsDimensions_RS));
?>

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
  Réponse avec citation
Vieux 03/10/2008, 09h15   #12
colinwalton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Converting cm to inches in a line of text

Hi again,

This works beautifully:

Imperial measurements: <php echo $row_getsDimensions_RS['dims']; ?><br />
Metric measurements: <php echo
convertDimensions($row_getsDimensions_RS['dims']); ?>

But ideally I would like to show them in different repeat regions, but (sorry)
I don't understand what you mean by this:

If you want to display the results in different repeat regions, you need
to rewind the recordset like this:

(rewind? = replace? / add to? where do I put this text?)

<?php
mysql_data_seek($getsDimensions_RS, 0);
$row_getsDimensions_RS = mysql_fetch_assoc($getsDimensions_RS);
do {
// reuse the recordset here
} while ($row_getsDimensions_RS = mysql_fetch_assoc($getsDimensions_RS));
?>

I have tried putting this in the document - nothing happens, then tried
putting it with the rest of the functions at the beginning of the document -
and again nothing happened. Can you possibly be a bit more specific as to where
I need to place this text? thanks.

  Réponse avec citation
Vieux 03/10/2008, 12h13   #13
David Powers
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Converting cm to inches in a line of text

colinwalton wrote:
> But ideally I would like to show them in different repeat regions, but (sorry)
> I don't understand what you mean by this:
>
> If you want to display the results in different repeat regions, you need
> to rewind the recordset like this:
>
> (rewind? = replace? / add to? where do I put this text?)


A recordset is like a fishing line. At the end of the repeat region, you
have reached the end of the line. You need to rewind it to get to the
beginning again.

The following code shows in outline what you do to rewind the recordset
and use it again. It goes at the point where you want to use the
recordset the second time.

> <?php
> mysql_data_seek($getsDimensions_RS, 0);
> $row_getsDimensions_RS = mysql_fetch_assoc($getsDimensions_RS);
> do {
> // reuse the recordset here
> } while ($row_getsDimensions_RS = mysql_fetch_assoc($getsDimensions_RS));
> ?>


> I have tried putting this in the document - nothing happens


If you have put it in the page exactly like that, of course nothing will
happen. The following line is a PHP comment:

// reuse the recordset here

You need to replace that with the code to display the results from the
recordset. Dreamweaver won't let you use a repeat region twice for the
same recordset. You need to code it by hand yourself.

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
  Réponse avec citation
Vieux 04/10/2008, 10h49   #14
colinwalton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Converting cm to inches in a line of text

Thanks for the explanation.

I now get this error message on the page:

Warning: mysql_data_seek() [function.mysql-data-seek]: Offset 0 is invalid for
MySQL result index 10 (or the query data is unbuffered) in
/secure/c/capitalgarden/product.php on line 214

This is the code as it stands in the div on the page...

<div id="middleProductDiv">
<table width="520" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center" valign="top"><a href="code.php?cat=<?php echo
$row_choosingProductRS['cat']; ?>"><img src="pics/<?php echo
$row_productsRS['code']; ?>.jpg" alt="<?php echo $product?>" title="click to
see more <?php echo $row_productsRS['cat']; ?>" border="0"></a></td>
</tr>
<tr>
<td align="center" valign="top"><p><span class="sectionTitle"><?php echo
$row_productsRS['product']; ?></span></td>
</tr>
<tr>
<td align="center" valign="top">&nbsp;</td>
</tr>
<tr>
<td align="center" valign="top"><?php if ($totalRows_productsRS > 0) {
// Show if recordset not empty ?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center"><?php echo $row_productsRS['add'];
?>
</td>
</tr>
</table>
<?php } // Show if recordset not empty ?></td>
</tr>
<tr>
<td align="center" valign="top">&nbsp;</td>
</tr>
<tr>
<td align="center" valign="top"><?php do { ?>
<table width="100%" border="0" cellpadding="0" cellspacing="0"
bgcolor="#CC0066">
<tr>
<td align="center" valign="top"><?php echo
$row_getsDimensions_RS['dims']; ?>&nbsp;&nbsp;&nbsp;&nbsp;<span
class="smallCaps">code</span>&nbsp;<?php echo
$row_getsDimensions_RS['code']; ?>
</td>
</tr>

<tr>
<td align="center" valign="top">&nbsp;</td>
</tr>
</table>
<?php } while ($row_getsDimensions_RS =
mysql_fetch_assoc($getsDimensions_RS)); ?></td>
</tr>
<tr>
<td align="center" valign="top"><?php do { ?>
<table width="100%" border="0" cellpadding="0" cellspacing="0"
bgcolor="#FF6633">
<tr>
<td align="center" valign="top"><span class="paleGreyText">
<?php
mysql_data_seek($getsDimensions_RS, 0);
$row_getsDimensions_RS = mysql_fetch_assoc($getsDimensions_RS);
do {
// reuse the recordset here

$colname_getsDimensions_RS = "-1";
if (isset($_GET['cat'])) {
$colname_getsDimensions_RS = $_GET['cat'];
}
$colname2_getsDimensions_RS = "-1";
if (isset($_GET['product'])) {
$colname2_getsDimensions_RS = $_GET['product'];
}
mysql_select_db($database_connectionCGP, $connectionCGP);
$query_getsDimensions_RS = sprintf("SELECT * FROM products WHERE cat = %s AND
product = %s ORDER BY dims DESC", GetSQLValueString($colname_getsDimensions_RS,
"text"),GetSQLValueString($colname2_getsDimensions _RS, "text"));
$getsDimensions_RS = mysql_query($query_getsDimensions_RS, $connectionCGP) or
die(mysql_error());
$row_getsDimensions_RS = mysql_fetch_assoc($getsDimensions_RS);
$totalRows_getsDimensions_RS = mysql_num_rows($getsDimensions_RS);

// end of the recordset here
} while ($row_getsDimensions_RS = mysql_fetch_assoc($getsDimensions_RS));
?>


<?php echo
convertDimensions($row_getsDimensions_RS['dims']);
?></span>&nbsp;&nbsp;&nbsp;&nbsp;<span
class="smallCaps">code</span>&nbsp;<?php echo
$row_getsDimensions_RS['code']; ?>
</td>
</tr>

<tr>
<td align="center" valign="top">&nbsp;</td>
</tr>
</table>
<?php } while ($row_getsDimensions_RS =
mysql_fetch_assoc($getsDimensions_RS)); ?></td>
</tr>
<tr>
<td align="center" valign="top">&nbsp;</td>
</tr>
</table>

</div>


  Réponse avec citation
Vieux 04/10/2008, 15h49   #15
David Powers
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Converting cm to inches in a line of text

colinwalton wrote:
> I now get this error message on the page:
>
> Warning: mysql_data_seek() [function.mysql-data-seek]: Offset 0 is invalid for
> MySQL result index 10 (or the query data is unbuffered) in
> /secure/c/capitalgarden/product.php on line 214


Hmm, you said you took on this project to learn PHP and MySQL.
You've obviously learned some, but you're clearly not understanding what
the code is doing or what it's for. Until you get a stronger grasp of
how PHP works, you will be flailing about in the dark. Fortunately, it's
not all that difficult. Once you know the basics of loops, you'll be
able to adapt the Dreamweaver code with little difficulty.

There are two problems with what you have done. The first is that you
have put my code inside a loop, whereas the code I gave you creates the
loop that you need. Secondly, you have misunderstood what I meant by
"reuse" the recordset. Instead of reusing it, you have created it again.
What's more it's now inside two loops, so the script would have gone
round in never-ending circles if it hadn't generated an error.

Here, in very simple terms is what you need to do:

<table>
<?php
// this is the first loop that uses the recordset in a repeat region
do {
?>
<tr>
<td>Metric sizes: <?php echo
convertDimensions($getsDimensions_RS['dims']); ?></td>
</tr>
<?php } while ($row_getsDimensions_RS =
mysql_fetch_assoc($getsDimensions_RS));
?>
</table>
<table>
<?php
// the getsDimensions_RS recordset has already been used
// in a repeat region, so you need to reset it
mysql_data_seek($getsDimensions_RS, 0);
// then get the first row
$row_getsDimensions_RS = mysql_fetch_assoc($getsDimensions_RS);
// now, loop through the recordset again
do {
?>
<tr>
<td>Imperial sizes: <?php echo $getsDimensions_RS['dims']; ?></td>
</tr>
<?php } while ($row_getsDimensions_RS =
mysql_fetch_assoc($getsDimensions_RS));
?>
</table>

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
  Réponse avec citation
Vieux 04/10/2008, 17h13   #16
colinwalton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Converting cm to inches in a line of text

- " Until you get a stronger grasp of how PHP works, you will be flailing
about in the dark." -

Sometimes if my students don't understand, I have to realise its a fault in my
teaching....

Whilst I am grateful for your , you must realise people come onto the
forum because they do NOT have a grasp of PHP.

I have had my enthusiasm somewhat dashed by your comments. Further exasperated
by the code you supplied in "simple terms" not working. The page now displays
this:

Metric sizes:
Metric sizes:
Metric sizes:
Imperial sizes:
Imperial sizes:
Imperial sizes:
CODE

To be honest I don't really expect a reply as I have clearly caught you on an
off day.

Thank you for all your in the past.

Regards

C

  Réponse avec citation
Vieux 04/10/2008, 18h16   #17
David Powers
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Converting cm to inches in a line of text

colinwalton wrote:
> Sometimes if my students don't understand, I have to realise its a fault in my
> teaching....


Yes, that's why I have stuck with you, trying to . The problem with
a forum is that it's impossible to know how much knowledge the other
person has.

> Whilst I am grateful for your , you must realise people come onto the
> forum because they do NOT have a grasp of PHP.


Everybody is a beginner at some stage.

> I have had my enthusiasm somewhat dashed by your comments. Further exasperated
> by the code you supplied in "simple terms" not working. The page now displays
> this:
>
> Metric sizes:
> Metric sizes:
> Metric sizes:
> Imperial sizes:
> Imperial sizes:
> Imperial sizes:


Actually, that shouldn't dismay you, but should be taken as a sign that
you're moving in the right direction. You want to use the same recordset
twice in two different tables. What you have got are the two tables, but
the values from the recordset haven't been displayed. What's needed is
to find out why the values aren't being displayed.

> To be honest I don't really expect a reply as I have clearly caught you on an
> off day.


No, I think it's you who's having the off day, because you're frustrated
(understandably so) that this isn't working.

The problem with the code you're posting here is that you have a complex
page, which gets in the way of understanding the basic principle of
what's going on.

Try a test page with a simple recordset that retrieves just one field.
For the sake of an example, call the recordset getDims, and retrieve the
dims field.

Next, create a table to display the dims field, and apply a repeat
region to it. This will give you the following code:

<table>
<?php
do {
?>
<tr>
<td>Original table: <?php echo $getDims['dims']); ?></td>
</tr>
<?php } while ($row_getDims = mysql_fetch_assoc($getDims));
?>
</table>

Make sure that it displays the field and repeat region correctly. Once
you have confirmed that, you can add the code to rewind the recordset,
and display it again. The two tables end up looking like this:

<table>
<?php
do {
?>
<tr>
<td>Original table: <?php echo $getDims['dims']); ?></td>
</tr>
<?php } while ($row_getDims = mysql_fetch_assoc($getDims));
?>
</table>
<table>
<?php
mysql_data_seek($getDims, 0);
$row_getDims = mysql_fetch_assoc($getDims);
do {
?>
<tr>
<td>Second table: <?php echo $getDims['dims']); ?></td>
</tr>
<?php } while ($row_getDims = mysql_fetch_assoc($getDims));
?>
</table>

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
  Réponse avec citation
Vieux 05/10/2008, 11h33   #18
colinwalton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Converting cm to inches in a line of text

Hi again, thanks for the encouragement!

Hoorah! finally got it to work - although I did have to take out a couple of
banana skins...
I had to change this line of code <?php echo $getDims['dims']); ?>
to this <?php echo
$row_getDims['dims']; ?>

It then works fine! thanks - job done. For ref here is the final code:

This goes at the end of the chunk of PHP before the <doctype>

function convertDimensions($dims) {
return preg_replace_callback('/(\d+)cm/',
create_function('$v', 'return round($v[1] * .394, 1)."in";'),
$dims
);
}
?><!DOCTYPE html PUBLIC....... etc

Then this goes in the body:

<table>
<?php
do {
?>
<tr>
<td>Original table: <?php echo $row_getDims['dims']; ?></td>
</tr>
<?php } while ($row_getDims = mysql_fetch_assoc($getDims));
?>
</table>
<table>
<?php
mysql_data_seek($getDims, 0);
$row_getDims = mysql_fetch_assoc($getDims);
do {
?>
<tr>
<td>Second table: <?php echo
convertDimensions($row_getDims['dims']); ?></td>
</tr>
<?php } while ($row_getDims = mysql_fetch_assoc($getDims));
?>
</table>


Thanks again C

  Réponse avec citation
Vieux 05/10/2008, 15h51   #19
David Powers
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Converting cm to inches in a line of text

colinwalton wrote:
> Hi again, thanks for the encouragement!
>
> Hoorah! finally got it to work - although I did have to take out a couple of
> banana skins...
> I had to change this line of code <?php echo $getDims['dims']); ?>
> to this <?php echo
> $row_getDims['dims']; ?>
>
> It then works fine! thanks - job done.


Great. Yes, banana skins are a feature of trying to assist in an online
forum. When typing out example code, it's easy to misspell something or
use the wrong variable. However, when you run the code, the error
usually leaps out fairly quickly. One of the main differences between an
"expert" and a beginner lies in the ability to know where to look for
the error. When I first began, it would sometimes take half an hour to
spot a missing semicolon. Now, I can usually identify the the problem in
seconds. However, it's only when you run the code that the error leaps out.

Reading huge swathes of code - particularly someone else's - is never
easy. That's why I suggested trying the simple approach first. Once you
understand the principle behind a technique, it's much easier to
incorporate into your more complex code.

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
  Réponse avec citation
Vieux 05/10/2008, 16h13   #20
colinwalton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Converting cm to inches in a line of text

No worries - thanks again for your .

Much appreciated.

c
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 00h46.


Édité par : vBulletin® version 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,44405 seconds with 28 queries